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; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 248cbbfdceSGreg Roachuse Fisharebest\Webtrees\Html; 250e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 279c6524dcSGreg Roachuse Fisharebest\Webtrees\View; 288c2e8227SGreg Roach 298c2e8227SGreg Roach/** 308c2e8227SGreg Roach * Class SlideShowModule 318c2e8227SGreg Roach */ 32e2a378d3SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface { 338c2e8227SGreg Roach /** {@inheritdoc} */ 348c2e8227SGreg Roach public function getTitle() { 358c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Slide show'); 368c2e8227SGreg Roach } 378c2e8227SGreg Roach 388c2e8227SGreg Roach /** {@inheritdoc} */ 398c2e8227SGreg Roach public function getDescription() { 408c2e8227SGreg Roach return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.'); 418c2e8227SGreg Roach } 428c2e8227SGreg Roach 4376692c8bSGreg Roach /** 4476692c8bSGreg Roach * Generate the HTML content of this block. 4576692c8bSGreg Roach * 4676692c8bSGreg Roach * @param int $block_id 4776692c8bSGreg Roach * @param bool $template 48727f238cSGreg Roach * @param string[] $cfg 4976692c8bSGreg Roach * 5076692c8bSGreg Roach * @return string 5176692c8bSGreg Roach */ 52a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 534b9ff166SGreg Roach global $ctype, $WT_TREE; 548c2e8227SGreg Roach 55e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 56e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 57e2a378d3SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start'); 588c2e8227SGreg Roach 598c2e8227SGreg Roach // We can apply the filters using SQL 608c2e8227SGreg Roach // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 618c2e8227SGreg Roach $all_media = Database::prepare( 628c2e8227SGreg Roach "SELECT m_id FROM `##media`" . 638f5f5da8SGreg Roach " JOIN `##media_file` USING (m_file, m_id)" . 648c2e8227SGreg Roach " WHERE m_file = ?" . 658f5f5da8SGreg Roach " AND multimedia_format IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 668f5f5da8SGreg Roach " AND source_media_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); 94572a608bSGreg Roach $media_file = $media->firstImageFile(); 95572a608bSGreg Roach if ($media->canShow() && $media !== null && !$media_file->isExternal()) { 968c2e8227SGreg Roach // Check if it is linked to a suitable individual 978c2e8227SGreg Roach foreach ($media->linkedIndividuals('OBJE') as $indi) { 988c2e8227SGreg Roach if ( 998c2e8227SGreg Roach $filter === 'all' || 1008c2e8227SGreg Roach $filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false || 1018c2e8227SGreg Roach $filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false 1028c2e8227SGreg Roach ) { 1038c2e8227SGreg Roach // Found one :-) 1048c2e8227SGreg Roach $random_media = $media; 1058c2e8227SGreg Roach break 2; 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach unset($all_media[$n]); 1103c12f3e5SGreg Roach } 1118c2e8227SGreg Roach 1128c2e8227SGreg Roach if ($random_media) { 113a1fe7073SGreg Roach $content = View::make('blocks/slide-show', [ 114a1fe7073SGreg Roach 'block_id' => $block_id, 115a1fe7073SGreg Roach 'media' => $random_media, 116572a608bSGreg Roach 'media_file' => $media_file, 117a1fe7073SGreg Roach 'show_controls' => $controls, 118a1fe7073SGreg Roach 'start_automatically' => $start, 119a1fe7073SGreg Roach ]); 1208c2e8227SGreg Roach } else { 1218c2e8227SGreg Roach $content = I18N::translate('This family tree has no images to display.'); 1228c2e8227SGreg Roach } 123a1fe7073SGreg Roach 1248c2e8227SGreg Roach if ($template) { 1258cbbfdceSGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 1268cbbfdceSGreg Roach $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 1278cbbfdceSGreg Roach } else { 1288cbbfdceSGreg Roach $config_url = ''; 1298cbbfdceSGreg Roach } 1308cbbfdceSGreg Roach 1319c6524dcSGreg Roach return View::make('blocks/template', [ 1329c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1339c6524dcSGreg Roach 'id' => $block_id, 1348cbbfdceSGreg Roach 'config_url' => $config_url, 1359c6524dcSGreg Roach 'title' => $this->getTitle(), 1369c6524dcSGreg Roach 'content' => $content, 1379c6524dcSGreg Roach ]); 1388c2e8227SGreg Roach } else { 1398c2e8227SGreg Roach return $content; 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach 1438c2e8227SGreg Roach /** {@inheritdoc} */ 144a9430be8SGreg Roach public function loadAjax(): bool { 1458c2e8227SGreg Roach return true; 1468c2e8227SGreg Roach } 1478c2e8227SGreg Roach 1488c2e8227SGreg Roach /** {@inheritdoc} */ 149a9430be8SGreg Roach public function isUserBlock(): bool { 1508c2e8227SGreg Roach return true; 1518c2e8227SGreg Roach } 1528c2e8227SGreg Roach 1538c2e8227SGreg Roach /** {@inheritdoc} */ 154a9430be8SGreg Roach public function isGedcomBlock(): bool { 1558c2e8227SGreg Roach return true; 1568c2e8227SGreg Roach } 1578c2e8227SGreg Roach 15876692c8bSGreg Roach /** 15976692c8bSGreg Roach * An HTML form to edit block settings 16076692c8bSGreg Roach * 16176692c8bSGreg Roach * @param int $block_id 162a9430be8SGreg Roach * 163a9430be8SGreg Roach * @return void 16476692c8bSGreg Roach */ 165*be9a728cSGreg Roach public function configureBlock($block_id) { 1668c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 167e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 168e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 169e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 170e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 171e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 172e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 173e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 174e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 175e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 176e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 177e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 178e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 179e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 180e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 181e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 182e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 183e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 184e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 185e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 186e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 187e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 1888c2e8227SGreg Roach } 1898c2e8227SGreg Roach 190e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 191e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 19215d603e7SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0'); 1938c2e8227SGreg Roach 19413abd6f3SGreg Roach $filters = [ 195e2a378d3SGreg Roach 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 196e2a378d3SGreg Roach 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 197e2a378d3SGreg Roach 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 198e2a378d3SGreg Roach 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 199e2a378d3SGreg Roach 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 200e2a378d3SGreg Roach 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 201e2a378d3SGreg Roach 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 202e2a378d3SGreg Roach 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 203e2a378d3SGreg Roach 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 204e2a378d3SGreg Roach 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 205e2a378d3SGreg Roach 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 206e2a378d3SGreg Roach 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 207e2a378d3SGreg Roach 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 208e2a378d3SGreg Roach 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 209e2a378d3SGreg Roach 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 210e2a378d3SGreg Roach 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 211e2a378d3SGreg Roach 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 212e2a378d3SGreg Roach 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 21313abd6f3SGreg Roach ]; 2148c2e8227SGreg Roach 2158c2e8227SGreg Roach ?> 21615d603e7SGreg Roach <div class="form-control row"> 21715d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="filter"> 21815d603e7SGreg Roach <?= I18N::translate('Show only individuals, events, or all') ?> 2198c2e8227SGreg Roach </label> 22015d603e7SGreg Roach <div class="col-sm-9"> 22115d603e7SGreg Roach <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 22215d603e7SGreg Roach </div> 22315d603e7SGreg Roach </div> 22415d603e7SGreg Roach 22515d603e7SGreg Roach <fieldset class="form-group"> 22615d603e7SGreg Roach <div class="row"> 22715d603e7SGreg Roach <legend class="col-form-legend col-sm-3"> 22815d603e7SGreg Roach <?= GedcomTag::getLabel('TYPE') ?> 22915d603e7SGreg Roach </legend> 23015d603e7SGreg Roach <div class="col-sm-9"> 23115d603e7SGreg Roach <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 23215d603e7SGreg Roach <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 23315d603e7SGreg Roach <?php endforeach ?> 23415d603e7SGreg Roach </div> 23515d603e7SGreg Roach </div> 23615d603e7SGreg Roach </fieldset> 23715d603e7SGreg Roach 23815d603e7SGreg Roach <div class="form-group row"> 23915d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="controls"> 24015d603e7SGreg Roach <?= I18N::translate('Show slide show controls') ?> 2418c2e8227SGreg Roach </label> 24215d603e7SGreg Roach <div class="col-sm-9"> 24315d603e7SGreg Roach <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 24415d603e7SGreg Roach </div> 24515d603e7SGreg Roach </div> 24615d603e7SGreg Roach 24715d603e7SGreg Roach <div class="form-group row"> 24815d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="start"> 24915d603e7SGreg Roach <?= I18N::translate('Start slide show on page load') ?> 2508c2e8227SGreg Roach </label> 25115d603e7SGreg Roach <div class="col-sm-9"> 25215d603e7SGreg Roach <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 25315d603e7SGreg Roach </div> 25415d603e7SGreg Roach </div> 2558c2e8227SGreg Roach <?php 2568c2e8227SGreg Roach } 2578c2e8227SGreg Roach} 258