18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter; 2215d603e7SGreg Roachuse Fisharebest\Webtrees\FontAwesome; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit; 243d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class SlideShowModule 328c2e8227SGreg Roach */ 33e2a378d3SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface { 348c2e8227SGreg Roach /** {@inheritdoc} */ 358c2e8227SGreg Roach public function getTitle() { 368c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Slide show'); 378c2e8227SGreg Roach } 388c2e8227SGreg Roach 398c2e8227SGreg Roach /** {@inheritdoc} */ 408c2e8227SGreg Roach public function getDescription() { 418c2e8227SGreg Roach return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.'); 428c2e8227SGreg Roach } 438c2e8227SGreg Roach 4476692c8bSGreg Roach /** 4576692c8bSGreg Roach * Generate the HTML content of this block. 4676692c8bSGreg Roach * 4776692c8bSGreg Roach * @param int $block_id 4876692c8bSGreg Roach * @param bool $template 49727f238cSGreg Roach * @param string[] $cfg 5076692c8bSGreg Roach * 5176692c8bSGreg Roach * @return string 5276692c8bSGreg Roach */ 5313abd6f3SGreg Roach public function getBlock($block_id, $template = true, $cfg = []) { 544b9ff166SGreg Roach global $ctype, $WT_TREE; 558c2e8227SGreg Roach 56e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 57e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 58e2a378d3SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start'); 598c2e8227SGreg Roach 608c2e8227SGreg Roach // We can apply the filters using SQL 618c2e8227SGreg Roach // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 628c2e8227SGreg Roach $all_media = Database::prepare( 638c2e8227SGreg Roach "SELECT m_id FROM `##media`" . 648c2e8227SGreg Roach " WHERE m_file = ?" . 6515d603e7SGreg Roach " AND m_ext IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 668c2e8227SGreg Roach " AND m_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" 6713abd6f3SGreg Roach )->execute([ 6824ec66ceSGreg Roach $WT_TREE->getTreeId(), 69e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 70e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 71e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 72e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 73e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 74e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 75e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 76e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 77e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 78e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 79e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 80e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 81e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 82e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 83e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 84e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 85e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 86e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 8713abd6f3SGreg Roach ])->fetchOneColumn(); 888c2e8227SGreg Roach 898c2e8227SGreg Roach // Keep looking through the media until a suitable one is found. 908c2e8227SGreg Roach $random_media = null; 918c2e8227SGreg Roach while ($all_media) { 928c2e8227SGreg Roach $n = array_rand($all_media); 9324ec66ceSGreg Roach $media = Media::getInstance($all_media[$n], $WT_TREE); 948c2e8227SGreg Roach if ($media->canShow() && !$media->isExternal()) { 958c2e8227SGreg Roach // Check if it is linked to a suitable individual 968c2e8227SGreg Roach foreach ($media->linkedIndividuals('OBJE') as $indi) { 978c2e8227SGreg Roach if ( 988c2e8227SGreg Roach $filter === 'all' || 998c2e8227SGreg Roach $filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false || 1008c2e8227SGreg Roach $filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false 1018c2e8227SGreg Roach ) { 1028c2e8227SGreg Roach // Found one :-) 1038c2e8227SGreg Roach $random_media = $media; 1048c2e8227SGreg Roach break 2; 1058c2e8227SGreg Roach } 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach unset($all_media[$n]); 1093c12f3e5SGreg Roach } 1108c2e8227SGreg Roach 1118c2e8227SGreg Roach $id = $this->getName() . $block_id; 1128c2e8227SGreg Roach $class = $this->getName() . '_block'; 1134b9ff166SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 11415d603e7SGreg Roach $title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' '; 1158c2e8227SGreg Roach } else { 1168c2e8227SGreg Roach $title = ''; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach $title .= $this->getTitle(); 1198c2e8227SGreg Roach 1208c2e8227SGreg Roach if ($random_media) { 12115d603e7SGreg Roach $content = '<div id="random_picture_container' . $block_id . '">'; 1228c2e8227SGreg Roach if ($controls) { 1238c2e8227SGreg Roach if ($start) { 1248c2e8227SGreg Roach $icon_class = 'icon-media-stop'; 1258c2e8227SGreg Roach } else { 1268c2e8227SGreg Roach $icon_class = 'icon-media-play'; 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach $content .= '<div dir="ltr" class="center" id="random_picture_controls' . $block_id . '"><br>'; 1297a6ee1acSGreg Roach $content .= '<a href="#" onclick="togglePlay(); return false;" id="play_stop" class="' . $icon_class . '" title="' . I18N::translate('Play') . '/' . I18N::translate('Stop') . '"></a>'; 13015d603e7SGreg Roach $content .= '<a href="#" onclick="$(\'#block_' . $block_id . '\').load(\'index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '\');return false;" title="' . I18N::translate('Next image') . '" class="icon-media-next"></a>'; 1318c2e8227SGreg Roach $content .= '</div><script> 1328c2e8227SGreg Roach var play = false; 1338c2e8227SGreg Roach function togglePlay() { 1348c2e8227SGreg Roach if (play) { 1358c2e8227SGreg Roach play = false; 13615d603e7SGreg Roach $("#play_stop").removeClass("icon-media-stop").addClass("icon-media-play"); 1378c2e8227SGreg Roach } 1388c2e8227SGreg Roach else { 1398c2e8227SGreg Roach play = true; 1408c2e8227SGreg Roach playSlideShow(); 14115d603e7SGreg Roach $("#play_stop").removeClass("icon-media-play").addClass("icon-media-stop"); 1428c2e8227SGreg Roach } 1438c2e8227SGreg Roach } 1448c2e8227SGreg Roach 1458c2e8227SGreg Roach function playSlideShow() { 1468c2e8227SGreg Roach if (play) { 1478c2e8227SGreg Roach window.setTimeout("reload_image()", 6000); 1488c2e8227SGreg Roach } 1498c2e8227SGreg Roach } 1508c2e8227SGreg Roach function reload_image() { 1518c2e8227SGreg Roach if (play) { 15215d603e7SGreg Roach $("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '&start=1"); 1538c2e8227SGreg Roach } 1548c2e8227SGreg Roach } 1558c2e8227SGreg Roach </script>'; 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach if ($start) { 1588c2e8227SGreg Roach $content .= '<script>togglePlay();</script>'; 1598c2e8227SGreg Roach } 1608c2e8227SGreg Roach $content .= '<div class="center" id="random_picture_content' . $block_id . '">'; 1618c2e8227SGreg Roach $content .= '<table id="random_picture_box"><tr><td class="details1">'; 162*dce07401SGreg Roach $content .= $random_media->displayImage(200, 200, '', []); 1638c2e8227SGreg Roach 1648c2e8227SGreg Roach $content .= '<br>'; 1658c2e8227SGreg Roach $content .= '<a href="' . $random_media->getHtmlUrl() . '"><b>' . $random_media->getFullName() . '</b></a><br>'; 1668c2e8227SGreg Roach foreach ($random_media->linkedIndividuals('OBJE') as $individual) { 167a759c7d4SGreg Roach $content .= '<a href="' . $individual->getHtmlUrl() . '">' . I18N::translate('View this individual') . ' — ' . $individual->getFullName() . '</a><br>'; 1688c2e8227SGreg Roach } 1698c2e8227SGreg Roach foreach ($random_media->linkedFamilies('OBJE') as $family) { 170a759c7d4SGreg Roach $content .= '<a href="' . $family->getHtmlUrl() . '">' . I18N::translate('View this family') . ' — ' . $family->getFullName() . '</a><br>'; 1718c2e8227SGreg Roach } 1728c2e8227SGreg Roach foreach ($random_media->linkedSources('OBJE') as $source) { 173a759c7d4SGreg Roach $content .= '<a href="' . $source->getHtmlUrl() . '">' . I18N::translate('View this source') . ' — ' . $source->getFullName() . '</a><br>'; 1748c2e8227SGreg Roach } 1758c2e8227SGreg Roach $content .= '<br><div class="indent">'; 1767a6ee1acSGreg Roach $content .= FunctionsPrint::printFactNotes($random_media->getGedcom(), '1', false); 1778c2e8227SGreg Roach $content .= '</div>'; 1788c2e8227SGreg Roach $content .= '</td></tr></table>'; 1798c2e8227SGreg Roach $content .= '</div>'; // random_picture_content 1808c2e8227SGreg Roach $content .= '</div>'; // random_picture_container 1818c2e8227SGreg Roach } else { 1828c2e8227SGreg Roach $content = I18N::translate('This family tree has no images to display.'); 1838c2e8227SGreg Roach } 1848c2e8227SGreg Roach if ($template) { 1854f8ecee1SGreg Roach return Theme::theme()->formatBlock($id, $title, $class, $content); 1868c2e8227SGreg Roach } else { 1878c2e8227SGreg Roach return $content; 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach } 1908c2e8227SGreg Roach 1918c2e8227SGreg Roach /** {@inheritdoc} */ 1928c2e8227SGreg Roach public function loadAjax() { 1938c2e8227SGreg Roach return true; 1948c2e8227SGreg Roach } 1958c2e8227SGreg Roach 1968c2e8227SGreg Roach /** {@inheritdoc} */ 1978c2e8227SGreg Roach public function isUserBlock() { 1988c2e8227SGreg Roach return true; 1998c2e8227SGreg Roach } 2008c2e8227SGreg Roach 2018c2e8227SGreg Roach /** {@inheritdoc} */ 2028c2e8227SGreg Roach public function isGedcomBlock() { 2038c2e8227SGreg Roach return true; 2048c2e8227SGreg Roach } 2058c2e8227SGreg Roach 20676692c8bSGreg Roach /** 20776692c8bSGreg Roach * An HTML form to edit block settings 20876692c8bSGreg Roach * 20976692c8bSGreg Roach * @param int $block_id 21076692c8bSGreg Roach */ 2118c2e8227SGreg Roach public function configureBlock($block_id) { 2128c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 213e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 214e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 215e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 216e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 217e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 218e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 219e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 220e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 221e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 222e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 223e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 224e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 225e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 226e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 227e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 228e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 229e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 230e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 231e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 232e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 233e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 2348c2e8227SGreg Roach } 2358c2e8227SGreg Roach 236e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 237e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 23815d603e7SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0'); 2398c2e8227SGreg Roach 24013abd6f3SGreg Roach $filters = [ 241e2a378d3SGreg Roach 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 242e2a378d3SGreg Roach 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 243e2a378d3SGreg Roach 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 244e2a378d3SGreg Roach 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 245e2a378d3SGreg Roach 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 246e2a378d3SGreg Roach 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 247e2a378d3SGreg Roach 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 248e2a378d3SGreg Roach 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 249e2a378d3SGreg Roach 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 250e2a378d3SGreg Roach 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 251e2a378d3SGreg Roach 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 252e2a378d3SGreg Roach 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 253e2a378d3SGreg Roach 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 254e2a378d3SGreg Roach 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 255e2a378d3SGreg Roach 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 256e2a378d3SGreg Roach 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 257e2a378d3SGreg Roach 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 258e2a378d3SGreg Roach 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 25913abd6f3SGreg Roach ]; 2608c2e8227SGreg Roach 2618c2e8227SGreg Roach ?> 26215d603e7SGreg Roach <div class="form-control row"> 26315d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="filter"> 26415d603e7SGreg Roach <?= I18N::translate('Show only individuals, events, or all') ?> 2658c2e8227SGreg Roach </label> 26615d603e7SGreg Roach <div class="col-sm-9"> 26715d603e7SGreg Roach <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 26815d603e7SGreg Roach </div> 26915d603e7SGreg Roach </div> 27015d603e7SGreg Roach 27115d603e7SGreg Roach <fieldset class="form-group"> 27215d603e7SGreg Roach <div class="row"> 27315d603e7SGreg Roach <legend class="col-form-legend col-sm-3"> 27415d603e7SGreg Roach <?= GedcomTag::getLabel('TYPE') ?> 27515d603e7SGreg Roach </legend> 27615d603e7SGreg Roach <div class="col-sm-9"> 27715d603e7SGreg Roach <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 27815d603e7SGreg Roach <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 27915d603e7SGreg Roach <?php endforeach ?> 28015d603e7SGreg Roach </div> 28115d603e7SGreg Roach </div> 28215d603e7SGreg Roach </fieldset> 28315d603e7SGreg Roach 28415d603e7SGreg Roach <div class="form-group row"> 28515d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="controls"> 28615d603e7SGreg Roach <?= I18N::translate('Show slide show controls') ?> 2878c2e8227SGreg Roach </label> 28815d603e7SGreg Roach <div class="col-sm-9"> 28915d603e7SGreg Roach <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 29015d603e7SGreg Roach </div> 29115d603e7SGreg Roach </div> 29215d603e7SGreg Roach 29315d603e7SGreg Roach <div class="form-group row"> 29415d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="start"> 29515d603e7SGreg Roach <?= I18N::translate('Start slide show on page load') ?> 2968c2e8227SGreg Roach </label> 29715d603e7SGreg Roach <div class="col-sm-9"> 29815d603e7SGreg Roach <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 29915d603e7SGreg Roach </div> 30015d603e7SGreg Roach </div> 3018c2e8227SGreg Roach <?php 3028c2e8227SGreg Roach } 3038c2e8227SGreg Roach} 304