18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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; 278c2e8227SGreg Roach 288c2e8227SGreg Roach/** 298c2e8227SGreg Roach * Class SlideShowModule 308c2e8227SGreg Roach */ 31e2a378d3SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface { 328c2e8227SGreg Roach /** {@inheritdoc} */ 338c2e8227SGreg Roach public function getTitle() { 348c2e8227SGreg Roach return /* I18N: Name of a module */ I18N::translate('Slide show'); 358c2e8227SGreg Roach } 368c2e8227SGreg Roach 378c2e8227SGreg Roach /** {@inheritdoc} */ 388c2e8227SGreg Roach public function getDescription() { 398c2e8227SGreg Roach return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.'); 408c2e8227SGreg Roach } 418c2e8227SGreg Roach 4276692c8bSGreg Roach /** 4376692c8bSGreg Roach * Generate the HTML content of this block. 4476692c8bSGreg Roach * 4576692c8bSGreg Roach * @param int $block_id 4676692c8bSGreg Roach * @param bool $template 47727f238cSGreg Roach * @param string[] $cfg 4876692c8bSGreg Roach * 4976692c8bSGreg Roach * @return string 5076692c8bSGreg Roach */ 51a9430be8SGreg Roach public function getBlock($block_id, $template = true, $cfg = []): string { 524b9ff166SGreg Roach global $ctype, $WT_TREE; 538c2e8227SGreg Roach 54e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 55e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 56e2a378d3SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start'); 578c2e8227SGreg Roach 588c2e8227SGreg Roach // We can apply the filters using SQL 598c2e8227SGreg Roach // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 608c2e8227SGreg Roach $all_media = Database::prepare( 618c2e8227SGreg Roach "SELECT m_id FROM `##media`" . 628f5f5da8SGreg Roach " JOIN `##media_file` USING (m_file, m_id)" . 638c2e8227SGreg Roach " WHERE m_file = ?" . 648f5f5da8SGreg Roach " AND multimedia_format IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 658f5f5da8SGreg Roach " AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" 6613abd6f3SGreg Roach )->execute([ 6724ec66ceSGreg Roach $WT_TREE->getTreeId(), 68e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 69e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 70e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 71e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 72e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 73e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 74e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 75e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 76e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 77e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 78e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 79e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 80e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 81e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 82e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 83e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 84e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 85e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 8613abd6f3SGreg Roach ])->fetchOneColumn(); 878c2e8227SGreg Roach 888c2e8227SGreg Roach // Keep looking through the media until a suitable one is found. 898c2e8227SGreg Roach $random_media = null; 908c2e8227SGreg Roach while ($all_media) { 918c2e8227SGreg Roach $n = array_rand($all_media); 9224ec66ceSGreg Roach $media = Media::getInstance($all_media[$n], $WT_TREE); 93572a608bSGreg Roach $media_file = $media->firstImageFile(); 94572a608bSGreg Roach if ($media->canShow() && $media !== null && !$media_file->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) { 112225e381fSGreg Roach $content = view('blocks/slide-show', [ 113a1fe7073SGreg Roach 'block_id' => $block_id, 114a1fe7073SGreg Roach 'media' => $random_media, 115572a608bSGreg Roach 'media_file' => $media_file, 116a1fe7073SGreg Roach 'show_controls' => $controls, 117a1fe7073SGreg Roach 'start_automatically' => $start, 118a1fe7073SGreg Roach ]); 1198c2e8227SGreg Roach } else { 1208c2e8227SGreg Roach $content = I18N::translate('This family tree has no images to display.'); 1218c2e8227SGreg Roach } 122a1fe7073SGreg Roach 1238c2e8227SGreg Roach if ($template) { 124*397e599aSGreg Roach if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) { 125*397e599aSGreg Roach $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 126*397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 127*397e599aSGreg Roach $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 1288cbbfdceSGreg Roach } else { 1298cbbfdceSGreg Roach $config_url = ''; 1308cbbfdceSGreg Roach } 1318cbbfdceSGreg Roach 132225e381fSGreg Roach return view('blocks/template', [ 1339c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 1349c6524dcSGreg Roach 'id' => $block_id, 1358cbbfdceSGreg Roach 'config_url' => $config_url, 1369c6524dcSGreg Roach 'title' => $this->getTitle(), 1379c6524dcSGreg Roach 'content' => $content, 1389c6524dcSGreg Roach ]); 1398c2e8227SGreg Roach } else { 1408c2e8227SGreg Roach return $content; 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach } 1438c2e8227SGreg Roach 1448c2e8227SGreg Roach /** {@inheritdoc} */ 145a9430be8SGreg Roach public function loadAjax(): bool { 1468c2e8227SGreg Roach return true; 1478c2e8227SGreg Roach } 1488c2e8227SGreg Roach 1498c2e8227SGreg Roach /** {@inheritdoc} */ 150a9430be8SGreg Roach public function isUserBlock(): bool { 1518c2e8227SGreg Roach return true; 1528c2e8227SGreg Roach } 1538c2e8227SGreg Roach 1548c2e8227SGreg Roach /** {@inheritdoc} */ 155a9430be8SGreg Roach public function isGedcomBlock(): bool { 1568c2e8227SGreg Roach return true; 1578c2e8227SGreg Roach } 1588c2e8227SGreg Roach 15976692c8bSGreg Roach /** 16076692c8bSGreg Roach * An HTML form to edit block settings 16176692c8bSGreg Roach * 16276692c8bSGreg Roach * @param int $block_id 163a9430be8SGreg Roach * 164a9430be8SGreg Roach * @return void 16576692c8bSGreg Roach */ 166be9a728cSGreg Roach public function configureBlock($block_id) { 1678c2e8227SGreg Roach if (Filter::postBool('save') && Filter::checkCsrf()) { 168e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 169e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 170e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 171e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 172e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 173e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 174e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 175e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 176e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 177e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 178e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 179e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 180e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 181e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 182e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 183e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 184e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 185e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 186e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 187e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 188e2a378d3SGreg Roach $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 1898c2e8227SGreg Roach } 1908c2e8227SGreg Roach 191e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 192e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 19315d603e7SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0'); 1948c2e8227SGreg Roach 19513abd6f3SGreg Roach $filters = [ 196e2a378d3SGreg Roach 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 197e2a378d3SGreg Roach 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 198e2a378d3SGreg Roach 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 199e2a378d3SGreg Roach 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 200e2a378d3SGreg Roach 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 201e2a378d3SGreg Roach 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 202e2a378d3SGreg Roach 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 203e2a378d3SGreg Roach 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 204e2a378d3SGreg Roach 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 205e2a378d3SGreg Roach 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 206e2a378d3SGreg Roach 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 207e2a378d3SGreg Roach 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 208e2a378d3SGreg Roach 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 209e2a378d3SGreg Roach 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 210e2a378d3SGreg Roach 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 211e2a378d3SGreg Roach 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 212e2a378d3SGreg Roach 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 213e2a378d3SGreg Roach 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 21413abd6f3SGreg Roach ]; 2158c2e8227SGreg Roach 2168c2e8227SGreg Roach ?> 21715d603e7SGreg Roach <div class="form-control row"> 21815d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="filter"> 21915d603e7SGreg Roach <?= I18N::translate('Show only individuals, events, or all') ?> 2208c2e8227SGreg Roach </label> 22115d603e7SGreg Roach <div class="col-sm-9"> 22215d603e7SGreg Roach <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 22315d603e7SGreg Roach </div> 22415d603e7SGreg Roach </div> 22515d603e7SGreg Roach 22615d603e7SGreg Roach <fieldset class="form-group"> 22715d603e7SGreg Roach <div class="row"> 228fff20713SGreg Roach <legend class="col-form-label col-sm-3"> 22915d603e7SGreg Roach <?= GedcomTag::getLabel('TYPE') ?> 23015d603e7SGreg Roach </legend> 23115d603e7SGreg Roach <div class="col-sm-9"> 23215d603e7SGreg Roach <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 23315d603e7SGreg Roach <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 23415d603e7SGreg Roach <?php endforeach ?> 23515d603e7SGreg Roach </div> 23615d603e7SGreg Roach </div> 23715d603e7SGreg Roach </fieldset> 23815d603e7SGreg Roach 23915d603e7SGreg Roach <div class="form-group row"> 24015d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="controls"> 24115d603e7SGreg Roach <?= I18N::translate('Show slide show controls') ?> 2428c2e8227SGreg Roach </label> 24315d603e7SGreg Roach <div class="col-sm-9"> 24415d603e7SGreg Roach <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 24515d603e7SGreg Roach </div> 24615d603e7SGreg Roach </div> 24715d603e7SGreg Roach 24815d603e7SGreg Roach <div class="form-group row"> 24915d603e7SGreg Roach <label class="col-sm-3 col-form-label" for="start"> 25015d603e7SGreg Roach <?= I18N::translate('Start slide show on page load') ?> 2518c2e8227SGreg Roach </label> 25215d603e7SGreg Roach <div class="col-sm-9"> 25315d603e7SGreg Roach <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 25415d603e7SGreg Roach </div> 25515d603e7SGreg Roach </div> 2568c2e8227SGreg Roach <?php 2578c2e8227SGreg Roach } 2588c2e8227SGreg Roach} 259