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