xref: /webtrees/app/Module/SlideShowModule.php (revision 3763c3f2dff6a4360b7b8810b6250e372dded805)
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 SlideShowModule
21 */
22class SlideShowModule extends Module implements ModuleBlockInterface {
23	/** {@inheritdoc} */
24	public function getTitle() {
25		return /* I18N: Name of a module */ I18N::translate('Slide show');
26	}
27
28	/** {@inheritdoc} */
29	public function getDescription() {
30		return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.');
31	}
32
33	/** {@inheritdoc} */
34	public function getBlock($block_id, $template = true, $cfg = null) {
35		global $ctype, $WT_TREE;
36
37		$filter   = get_block_setting($block_id, 'filter', 'all');
38		$controls = get_block_setting($block_id, 'controls', '1');
39		$start    = get_block_setting($block_id, 'start', '0') || Filter::getBool('start');
40
41		// We can apply the filters using SQL
42		// Do not use "ORDER BY RAND()" - it is very slow on large tables.  Use PHP::array_rand() instead.
43		$all_media = Database::prepare(
44			"SELECT m_id FROM `##media`" .
45			" WHERE m_file = ?" .
46			" AND m_ext  IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" .
47			" AND m_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')"
48		)->execute(array(
49			WT_GED_ID,
50			get_block_setting($block_id, 'filter_avi', '0') ? 'avi' : null,
51			get_block_setting($block_id, 'filter_bmp', '1') ? 'bmp' : null,
52			get_block_setting($block_id, 'filter_gif', '1') ? 'gif' : null,
53			get_block_setting($block_id, 'filter_jpeg', '1') ? 'jpg' : null,
54			get_block_setting($block_id, 'filter_jpeg', '1') ? 'jpeg' : null,
55			get_block_setting($block_id, 'filter_mp3', '0') ? 'mp3' : null,
56			get_block_setting($block_id, 'filter_ole', '1') ? 'ole' : null,
57			get_block_setting($block_id, 'filter_pcx', '1') ? 'pcx' : null,
58			get_block_setting($block_id, 'filter_pdf', '0') ? 'pdf' : null,
59			get_block_setting($block_id, 'filter_png', '1') ? 'png' : null,
60			get_block_setting($block_id, 'filter_tiff', '1') ? 'tiff' : null,
61			get_block_setting($block_id, 'filter_wav', '0') ? 'wav' : null,
62			get_block_setting($block_id, 'filter_audio', '0') ? 'audio' : null,
63			get_block_setting($block_id, 'filter_book', '1') ? 'book' : null,
64			get_block_setting($block_id, 'filter_card', '1') ? 'card' : null,
65			get_block_setting($block_id, 'filter_certificate', '1') ? 'certificate' : null,
66			get_block_setting($block_id, 'filter_coat', '1') ? 'coat' : null,
67			get_block_setting($block_id, 'filter_document', '1') ? 'document' : null,
68			get_block_setting($block_id, 'filter_electronic', '1') ? 'electronic' : null,
69			get_block_setting($block_id, 'filter_fiche', '1') ? 'fiche' : null,
70			get_block_setting($block_id, 'filter_film', '1') ? 'film' : null,
71			get_block_setting($block_id, 'filter_magazine', '1') ? 'magazine' : null,
72			get_block_setting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null,
73			get_block_setting($block_id, 'filter_map', '1') ? 'map' : null,
74			get_block_setting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null,
75			get_block_setting($block_id, 'filter_other', '1') ? 'other' : null,
76			get_block_setting($block_id, 'filter_painting', '1') ? 'painting' : null,
77			get_block_setting($block_id, 'filter_photo', '1') ? 'photo' : null,
78			get_block_setting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null,
79			get_block_setting($block_id, 'filter_video', '0') ? 'video' : null,
80		))->fetchOneColumn();
81
82		// Keep looking through the media until a suitable one is found.
83		$random_media = null;
84		while ($all_media) {
85			$n = array_rand($all_media);
86			$media = Media::getInstance($all_media[$n]);
87			if ($media->canShow() && !$media->isExternal()) {
88				// Check if it is linked to a suitable individual
89				foreach ($media->linkedIndividuals('OBJE') as $indi) {
90					if (
91						$filter === 'all' ||
92						$filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false ||
93						$filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false
94					) {
95						// Found one :-)
96						$random_media = $media;
97						break 2;
98					}
99				}
100			}
101			unset($all_media[$n]);
102		};
103
104		$id = $this->getName() . $block_id;
105		$class = $this->getName() . '_block';
106		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
107			$title = '<i class="icon-admin" title="' . I18N::translate('Configure') . '" onclick="modalDialog(\'block_edit.php?block_id=' . $block_id . '\', \'' . $this->getTitle() . '\');"></i>';
108		} else {
109			$title = '';
110		}
111		$title .= $this->getTitle();
112
113		if ($random_media) {
114			$content = "<div id=\"random_picture_container$block_id\">";
115			if ($controls) {
116				if ($start) {
117					$icon_class = 'icon-media-stop';
118				} else {
119					$icon_class = 'icon-media-play';
120				}
121				$content .= '<div dir="ltr" class="center" id="random_picture_controls' . $block_id . '"><br>';
122				$content .= "<a href=\"#\" onclick=\"togglePlay(); return false;\" id=\"play_stop\" class=\"" . $icon_class . "\" title=\"" . I18N::translate('Play') . "/" . I18N::translate('Stop') . '"></a>';
123				$content .= '<a href="#" onclick="jQuery(\'#block_' . $block_id . '\').load(\'index.php?ctype=' . $ctype . '&amp;action=ajax&amp;block_id=' . $block_id . '\');return false;" title="' . I18N::translate('Next image') . '" class="icon-media-next"></a>';
124				$content .= '</div><script>
125					var play = false;
126						function togglePlay() {
127							if (play) {
128								play = false;
129								jQuery("#play_stop").removeClass("icon-media-stop").addClass("icon-media-play");
130							}
131							else {
132								play = true;
133								playSlideShow();
134								jQuery("#play_stop").removeClass("icon-media-play").addClass("icon-media-stop");
135							}
136						}
137
138						function playSlideShow() {
139							if (play) {
140								window.setTimeout("reload_image()", 6000);
141							}
142						}
143						function reload_image() {
144							if (play) {
145								jQuery("#block_'.$block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '&start=1");
146							}
147						}
148					</script>';
149			}
150			if ($start) {
151				$content .= '<script>togglePlay();</script>';
152			}
153			$content .= '<div class="center" id="random_picture_content' . $block_id . '">';
154			$content .= '<table id="random_picture_box"><tr><td class="details1">';
155			$content .= $random_media->displayImage();
156
157			$content .= '<br>';
158			$content .= '<a href="' . $random_media->getHtmlUrl() . '"><b>' . $random_media->getFullName() . '</b></a><br>';
159			foreach ($random_media->linkedIndividuals('OBJE') as $individual) {
160				$content .= '<a href="' . $individual->getHtmlUrl() . '">' . I18N::translate('View individual') . ' — ' . $individual->getFullname() . '</a><br>';
161			}
162			foreach ($random_media->linkedFamilies('OBJE') as $family) {
163				$content .= '<a href="' . $family->getHtmlUrl() . '">' . I18N::translate('View family') . ' — ' . $family->getFullname() . '</a><br>';
164			}
165			foreach ($random_media->linkedSources('OBJE') as $source) {
166				$content .= '<a href="' . $source->getHtmlUrl() . '">' . I18N::translate('View source') . ' — ' . $source->getFullname() . '</a><br>';
167			}
168			$content .= '<br><div class="indent">';
169			$content .= print_fact_notes($random_media->getGedcom(), "1", false);
170			$content .= '</div>';
171			$content .= '</td></tr></table>';
172			$content .= '</div>'; // random_picture_content
173			$content .= '</div>'; // random_picture_container
174		} else {
175			$content = I18N::translate('This family tree has no images to display.');
176		}
177		if ($template) {
178			echo Theme::theme()->formatBlock($id, $title, $class, $content);
179		} else {
180			return $content;
181		}
182	}
183
184	/** {@inheritdoc} */
185	public function loadAjax() {
186		return true;
187	}
188
189	/** {@inheritdoc} */
190	public function isUserBlock() {
191		return true;
192	}
193
194	/** {@inheritdoc} */
195	public function isGedcomBlock() {
196		return true;
197	}
198
199	/** {@inheritdoc} */
200	public function configureBlock($block_id) {
201		if (Filter::postBool('save') && Filter::checkCsrf()) {
202			set_block_setting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all'));
203			set_block_setting($block_id, 'controls', Filter::postBool('controls'));
204			set_block_setting($block_id, 'start', Filter::postBool('start'));
205			set_block_setting($block_id, 'filter_avi', Filter::postBool('filter_avi'));
206			set_block_setting($block_id, 'filter_bmp', Filter::postBool('filter_bmp'));
207			set_block_setting($block_id, 'filter_gif', Filter::postBool('filter_gif'));
208			set_block_setting($block_id, 'filter_jpeg', Filter::postBool('filter_jpeg'));
209			set_block_setting($block_id, 'filter_mp3', Filter::postBool('filter_mp3'));
210			set_block_setting($block_id, 'filter_ole', Filter::postBool('filter_ole'));
211			set_block_setting($block_id, 'filter_pcx', Filter::postBool('filter_pcx'));
212			set_block_setting($block_id, 'filter_pdf', Filter::postBool('filter_pdf'));
213			set_block_setting($block_id, 'filter_png', Filter::postBool('filter_png'));
214			set_block_setting($block_id, 'filter_tiff', Filter::postBool('filter_tiff'));
215			set_block_setting($block_id, 'filter_wav', Filter::postBool('filter_wav'));
216			set_block_setting($block_id, 'filter_audio', Filter::postBool('filter_audio'));
217			set_block_setting($block_id, 'filter_book', Filter::postBool('filter_book'));
218			set_block_setting($block_id, 'filter_card', Filter::postBool('filter_card'));
219			set_block_setting($block_id, 'filter_certificate', Filter::postBool('filter_certificate'));
220			set_block_setting($block_id, 'filter_coat', Filter::postBool('filter_coat'));
221			set_block_setting($block_id, 'filter_document', Filter::postBool('filter_document'));
222			set_block_setting($block_id, 'filter_electronic', Filter::postBool('filter_electronic'));
223			set_block_setting($block_id, 'filter_fiche', Filter::postBool('filter_fiche'));
224			set_block_setting($block_id, 'filter_film', Filter::postBool('filter_film'));
225			set_block_setting($block_id, 'filter_magazine', Filter::postBool('filter_magazine'));
226			set_block_setting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript'));
227			set_block_setting($block_id, 'filter_map', Filter::postBool('filter_map'));
228			set_block_setting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper'));
229			set_block_setting($block_id, 'filter_other', Filter::postBool('filter_other'));
230			set_block_setting($block_id, 'filter_painting', Filter::postBool('filter_painting'));
231			set_block_setting($block_id, 'filter_photo', Filter::postBool('filter_photo'));
232			set_block_setting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone'));
233			set_block_setting($block_id, 'filter_video', Filter::postBool('filter_video'));
234		}
235
236		$filter   = get_block_setting($block_id, 'filter', 'all');
237		$controls = get_block_setting($block_id, 'controls', '1');
238		$start    = get_block_setting($block_id, 'start', '0') || Filter::getBool('start');
239
240		echo '<tr><td class="descriptionbox wrap width33">';
241		echo I18N::translate('Show only individuals, events, or all?');
242		echo '</td><td class="optionbox">';
243		echo select_edit_control('filter', array('indi'=> I18N::translate('Individuals'), 'event'=> I18N::translate('Facts and events'), 'all'=> I18N::translate('All')), null, $filter, '');
244		echo '</td></tr>';
245
246		$filters = array(
247			'avi'        =>get_block_setting($block_id, 'filter_avi', '0'),
248			'bmp'        =>get_block_setting($block_id, 'filter_bmp', '1'),
249			'gif'        =>get_block_setting($block_id, 'filter_gif', '1'),
250			'jpeg'       =>get_block_setting($block_id, 'filter_jpeg', '1'),
251			'mp3'        =>get_block_setting($block_id, 'filter_mp3', '0'),
252			'ole'        =>get_block_setting($block_id, 'filter_ole', '1'),
253			'pcx'        =>get_block_setting($block_id, 'filter_pcx', '1'),
254			'pdf'        =>get_block_setting($block_id, 'filter_pdf', '0'),
255			'png'        =>get_block_setting($block_id, 'filter_png', '1'),
256			'tiff'       =>get_block_setting($block_id, 'filter_tiff', '1'),
257			'wav'        =>get_block_setting($block_id, 'filter_wav', '0'),
258			'audio'      =>get_block_setting($block_id, 'filter_audio', '0'),
259			'book'       =>get_block_setting($block_id, 'filter_book', '1'),
260			'card'       =>get_block_setting($block_id, 'filter_card', '1'),
261			'certificate'=>get_block_setting($block_id, 'filter_certificate', '1'),
262			'coat'       =>get_block_setting($block_id, 'filter_coat', '1'),
263			'document'   =>get_block_setting($block_id, 'filter_document', '1'),
264			'electronic' =>get_block_setting($block_id, 'filter_electronic', '1'),
265			'fiche'      =>get_block_setting($block_id, 'filter_fiche', '1'),
266			'film'       =>get_block_setting($block_id, 'filter_film', '1'),
267			'magazine'   =>get_block_setting($block_id, 'filter_magazine', '1'),
268			'manuscript' =>get_block_setting($block_id, 'filter_manuscript', '1'),
269			'map'        =>get_block_setting($block_id, 'filter_map', '1'),
270			'newspaper'  =>get_block_setting($block_id, 'filter_newspaper', '1'),
271			'other'      =>get_block_setting($block_id, 'filter_other', '1'),
272			'painting'   =>get_block_setting($block_id, 'filter_painting', '1'),
273			'photo'      =>get_block_setting($block_id, 'filter_photo', '1'),
274			'tombstone'  =>get_block_setting($block_id, 'filter_tombstone', '1'),
275			'video'      =>get_block_setting($block_id, 'filter_video', '0'),
276		);
277
278		echo '<tr><td class="descriptionbox wrap width33">';
279		echo I18N::translate('Filter');
280		?>
281	</td>
282	<td class="optionbox">
283		<center><b><?php echo GedcomTag::getLabel('FORM'); ?></b></center>
284		<table class="width100">
285			<tr>
286				<td class="width33">
287					<label>
288						<input type="checkbox" value="yes" name="filter_avi" <?php echo $filters['avi'] ? 'checked' : ''; ?>>
289						avi
290				</td>
291				<td class="width33">
292					<label>
293						<input type="checkbox" value="yes" name="filter_bmp" <?php echo $filters['bmp'] ? 'checked' : ''; ?>>
294						bmp
295					</label>
296				</td>
297				<td class="width33">
298					<label>
299						<input type="checkbox" value="yes" name="filter_gif" <?php echo $filters['gif'] ? 'checked' : ''; ?>>
300						gif
301					</label>
302				</td>
303			</tr>
304			<tr>
305				<td class="width33">
306					<label>
307						<input type="checkbox" value="yes" name="filter_jpeg" <?php echo $filters['jpeg'] ? 'checked' : ''; ?>>
308						jpeg
309					</label>
310				</td>
311				<td class="width33">
312					<label>
313						<input type="checkbox" value="yes" name="filter_mp3" <?php echo $filters['mp3'] ? 'checked' : ''; ?>>
314						mp3
315					</label>
316				</td>
317					<td class="width33">
318					<label>
319						<input type="checkbox" value="yes" name="filter_ole" <?php echo $filters['ole'] ? 'checked' : ''; ?>>
320						ole
321					</label>
322				</td>
323			</tr>
324			<tr>
325				<td class="width33">
326					<label>
327						<input type="checkbox" value="yes" name="filter_pcx" <?php echo $filters['pcx'] ? 'checked' : ''; ?>>
328						pcx
329					</label>
330				</td>
331				<td class="width33">
332					<label>
333						<input type="checkbox" value="yes" name="filter_pdf" <?php echo $filters['pdf'] ? 'checked' : ''; ?>>
334						pdf
335					</label>
336				</td>
337				<td class="width33">
338					<label>
339						<input type="checkbox" value="yes" name="filter_png" <?php echo $filters['png'] ? 'checked' : ''; ?>>
340						png
341					</label>
342				</td>
343			</tr>
344			<tr>
345				<td class="width33">
346					<label>
347						<input type="checkbox" value="yes" name="filter_tiff" <?php echo $filters['tiff'] ? 'checked' : ''; ?>>
348						tiff
349					</label>
350				</td>
351				<td class="width33">
352					<label>
353						<input type="checkbox" value="yes" name="filter_wav" <?php echo $filters['wav'] ? 'checked' : ''; ?>>
354						wav
355					</label>
356				</td>
357				<td class="width33"></td>
358				<td class="width33"></td>
359			</tr>
360		</table>
361			<br>
362			<center><b><?php echo GedcomTag::getLabel('TYPE'); ?></b></center>
363				<table class="width100">
364					<tr>
365					<?php
366					//-- Build the list of checkboxes
367					$i = 0;
368					foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue) {
369						$i++;
370						if ($i > 3) {
371							$i = 1;
372							echo '</tr><tr>';
373						}
374						echo '<td class="width33"><label><input type="checkbox" value="yes" name="filter_' . $typeName . '" ';
375						echo ($filters[$typeName]) ? 'checked' : '';
376						echo '> ' . $typeValue . '</label></td>';
377					}
378					?>
379				</tr>
380			</table>
381		</td>
382	</tr>
383
384	<?php
385
386		echo '<tr><td class="descriptionbox wrap width33">';
387		echo I18N::translate('Show slide show controls?');
388		echo '</td><td class="optionbox">';
389		echo edit_field_yes_no('controls', $controls);
390		echo '</td></tr>';
391
392		echo '<tr><td class="descriptionbox wrap width33">';
393		echo I18N::translate('Start slide show on page load?');
394		echo '</td><td class="optionbox">';
395		echo edit_field_yes_no('start', $start);
396		echo '</td></tr>';
397	}
398}
399