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; 223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit; 233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 25*8cbbfdceSGreg Roachuse Fisharebest\Webtrees\Html; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 270e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 289c6524dcSGreg Roachuse Fisharebest\Webtrees\View; 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 if ($random_media) { 11215d603e7SGreg Roach $content = '<div id="random_picture_container' . $block_id . '">'; 1138c2e8227SGreg Roach if ($controls) { 1148c2e8227SGreg Roach if ($start) { 1158c2e8227SGreg Roach $icon_class = 'icon-media-stop'; 1168c2e8227SGreg Roach } else { 1178c2e8227SGreg Roach $icon_class = 'icon-media-play'; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach $content .= '<div dir="ltr" class="center" id="random_picture_controls' . $block_id . '"><br>'; 1207a6ee1acSGreg Roach $content .= '<a href="#" onclick="togglePlay(); return false;" id="play_stop" class="' . $icon_class . '" title="' . I18N::translate('Play') . '/' . I18N::translate('Stop') . '"></a>'; 12115d603e7SGreg 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>'; 1228c2e8227SGreg Roach $content .= '</div><script> 1238c2e8227SGreg Roach var play = false; 1248c2e8227SGreg Roach function togglePlay() { 1258c2e8227SGreg Roach if (play) { 1268c2e8227SGreg Roach play = false; 12715d603e7SGreg Roach $("#play_stop").removeClass("icon-media-stop").addClass("icon-media-play"); 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach else { 1308c2e8227SGreg Roach play = true; 1318c2e8227SGreg Roach playSlideShow(); 13215d603e7SGreg Roach $("#play_stop").removeClass("icon-media-play").addClass("icon-media-stop"); 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach 1368c2e8227SGreg Roach function playSlideShow() { 1378c2e8227SGreg Roach if (play) { 1388c2e8227SGreg Roach window.setTimeout("reload_image()", 6000); 1398c2e8227SGreg Roach } 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach function reload_image() { 1428c2e8227SGreg Roach if (play) { 14315d603e7SGreg Roach $("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '&start=1"); 1448c2e8227SGreg Roach } 1458c2e8227SGreg Roach } 1468c2e8227SGreg Roach </script>'; 1478c2e8227SGreg Roach } 1488c2e8227SGreg Roach if ($start) { 1498c2e8227SGreg Roach $content .= '<script>togglePlay();</script>'; 1508c2e8227SGreg Roach } 1518c2e8227SGreg Roach $content .= '<div class="center" id="random_picture_content' . $block_id . '">'; 1528c2e8227SGreg Roach $content .= '<table id="random_picture_box"><tr><td class="details1">'; 153dce07401SGreg Roach $content .= $random_media->displayImage(200, 200, '', []); 1548c2e8227SGreg Roach 1558c2e8227SGreg Roach $content .= '<br>'; 1568c2e8227SGreg Roach $content .= '<a href="' . $random_media->getHtmlUrl() . '"><b>' . $random_media->getFullName() . '</b></a><br>'; 1578c2e8227SGreg Roach foreach ($random_media->linkedIndividuals('OBJE') as $individual) { 158a759c7d4SGreg Roach $content .= '<a href="' . $individual->getHtmlUrl() . '">' . I18N::translate('View this individual') . ' — ' . $individual->getFullName() . '</a><br>'; 1598c2e8227SGreg Roach } 1608c2e8227SGreg Roach foreach ($random_media->linkedFamilies('OBJE') as $family) { 161a759c7d4SGreg Roach $content .= '<a href="' . $family->getHtmlUrl() . '">' . I18N::translate('View this family') . ' — ' . $family->getFullName() . '</a><br>'; 1628c2e8227SGreg Roach } 1638c2e8227SGreg Roach foreach ($random_media->linkedSources('OBJE') as $source) { 164a759c7d4SGreg Roach $content .= '<a href="' . $source->getHtmlUrl() . '">' . I18N::translate('View this source') . ' — ' . $source->getFullName() . '</a><br>'; 1658c2e8227SGreg Roach } 1668c2e8227SGreg Roach $content .= '<br><div class="indent">'; 1677a6ee1acSGreg Roach $content .= FunctionsPrint::printFactNotes($random_media->getGedcom(), '1', false); 1688c2e8227SGreg Roach $content .= '</div>'; 1698c2e8227SGreg Roach $content .= '</td></tr></table>'; 1708c2e8227SGreg Roach $content .= '</div>'; // random_picture_content 1718c2e8227SGreg Roach $content .= '</div>'; // random_picture_container 1728c2e8227SGreg Roach } else { 1738c2e8227SGreg Roach $content = I18N::translate('This family tree has no images to display.'); 1748c2e8227SGreg Roach } 1758c2e8227SGreg Roach if ($template) { 176*8cbbfdceSGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 177*8cbbfdceSGreg Roach $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 178*8cbbfdceSGreg Roach } else { 179*8cbbfdceSGreg Roach $config_url = ''; 180*8cbbfdceSGreg Roach } 181*8cbbfdceSGreg Roach 1829c6524dcSGreg Roach return View::make('blocks/template', [ 1839c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1849c6524dcSGreg Roach 'id' => $block_id, 185*8cbbfdceSGreg Roach 'config_url' => $config_url, 1869c6524dcSGreg Roach 'title' => $this->getTitle(), 1879c6524dcSGreg Roach 'content' => $content, 1889c6524dcSGreg Roach ]); 1898c2e8227SGreg Roach } else { 1908c2e8227SGreg Roach return $content; 1918c2e8227SGreg Roach } 1928c2e8227SGreg Roach } 1938c2e8227SGreg Roach 1948c2e8227SGreg Roach /** {@inheritdoc} */ 1958c2e8227SGreg Roach public function loadAjax() { 1968c2e8227SGreg Roach return true; 1978c2e8227SGreg Roach } 1988c2e8227SGreg Roach 1998c2e8227SGreg Roach /** {@inheritdoc} */ 2008c2e8227SGreg Roach public function isUserBlock() { 2018c2e8227SGreg Roach return true; 2028c2e8227SGreg Roach } 2038c2e8227SGreg Roach 2048c2e8227SGreg Roach /** {@inheritdoc} */ 2058c2e8227SGreg Roach public function isGedcomBlock() { 2068c2e8227SGreg Roach return true; 2078c2e8227SGreg Roach } 2088c2e8227SGreg Roach 20976692c8bSGreg Roach /** 21076692c8bSGreg Roach * An HTML form to edit block settings 21176692c8bSGreg Roach * 21276692c8bSGreg Roach * @param int $block_id 21376692c8bSGreg Roach */ 2148c2e8227SGreg Roach public function configureBlock($block_id) { 2158c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 216e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 217e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 218e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 219e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 220e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 221e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 222e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 223e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 224e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 225e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 226e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 227e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 228e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 229e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 230e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 231e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 232e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 233e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 234e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 235e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 236e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 2378c2e8227SGreg Roach } 2388c2e8227SGreg Roach 239e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 240e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 24115d603e7SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0'); 2428c2e8227SGreg Roach 24313abd6f3SGreg Roach $filters = [ 244e2a378d3SGreg Roach 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 245e2a378d3SGreg Roach 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 246e2a378d3SGreg Roach 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 247e2a378d3SGreg Roach 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 248e2a378d3SGreg Roach 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 249e2a378d3SGreg Roach 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 250e2a378d3SGreg Roach 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 251e2a378d3SGreg Roach 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 252e2a378d3SGreg Roach 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 253e2a378d3SGreg Roach 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 254e2a378d3SGreg Roach 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 255e2a378d3SGreg Roach 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 256e2a378d3SGreg Roach 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 257e2a378d3SGreg Roach 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 258e2a378d3SGreg Roach 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 259e2a378d3SGreg Roach 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 260e2a378d3SGreg Roach 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 261e2a378d3SGreg Roach 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 26213abd6f3SGreg Roach ]; 2638c2e8227SGreg Roach 2648c2e8227SGreg Roach ?> 26515d603e7SGreg Roach <div class="form-control row"> 26615d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="filter"> 26715d603e7SGreg Roach <?= I18N::translate('Show only individuals, events, or all') ?> 2688c2e8227SGreg Roach </label> 26915d603e7SGreg Roach <div class="col-sm-9"> 27015d603e7SGreg Roach <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 27115d603e7SGreg Roach </div> 27215d603e7SGreg Roach </div> 27315d603e7SGreg Roach 27415d603e7SGreg Roach <fieldset class="form-group"> 27515d603e7SGreg Roach <div class="row"> 27615d603e7SGreg Roach <legend class="col-form-legend col-sm-3"> 27715d603e7SGreg Roach <?= GedcomTag::getLabel('TYPE') ?> 27815d603e7SGreg Roach </legend> 27915d603e7SGreg Roach <div class="col-sm-9"> 28015d603e7SGreg Roach <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 28115d603e7SGreg Roach <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 28215d603e7SGreg Roach <?php endforeach ?> 28315d603e7SGreg Roach </div> 28415d603e7SGreg Roach </div> 28515d603e7SGreg Roach </fieldset> 28615d603e7SGreg Roach 28715d603e7SGreg Roach <div class="form-group row"> 28815d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="controls"> 28915d603e7SGreg Roach <?= I18N::translate('Show slide show controls') ?> 2908c2e8227SGreg Roach </label> 29115d603e7SGreg Roach <div class="col-sm-9"> 29215d603e7SGreg Roach <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 29315d603e7SGreg Roach </div> 29415d603e7SGreg Roach </div> 29515d603e7SGreg Roach 29615d603e7SGreg Roach <div class="form-group row"> 29715d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="start"> 29815d603e7SGreg Roach <?= I18N::translate('Start slide show on page load') ?> 2998c2e8227SGreg Roach </label> 30015d603e7SGreg Roach <div class="col-sm-9"> 30115d603e7SGreg Roach <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 30215d603e7SGreg Roach </div> 30315d603e7SGreg Roach </div> 3048c2e8227SGreg Roach <?php 3058c2e8227SGreg Roach } 3068c2e8227SGreg Roach} 307