1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\Bootstrap4; 20use Fisharebest\Webtrees\Database; 21use Fisharebest\Webtrees\Filter; 22use Fisharebest\Webtrees\Functions\FunctionsEdit; 23use Fisharebest\Webtrees\GedcomTag; 24use Fisharebest\Webtrees\Html; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Media; 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 = []): string { 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 " JOIN `##media_file` USING (m_file, m_id)" . 63 " WHERE m_file = ?" . 64 " AND multimedia_format IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 65 " AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" 66 )->execute([ 67 $WT_TREE->getTreeId(), 68 $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 69 $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 70 $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 71 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 72 $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 73 $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 74 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 75 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 76 $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 77 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 78 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 79 $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 80 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 81 $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 82 $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 83 $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 84 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 85 $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 86 ])->fetchOneColumn(); 87 88 // Keep looking through the media until a suitable one is found. 89 $random_media = null; 90 while ($all_media) { 91 $n = array_rand($all_media); 92 $media = Media::getInstance($all_media[$n], $WT_TREE); 93 $media_file = $media->firstImageFile(); 94 if ($media->canShow() && $media !== null && !$media_file->isExternal()) { 95 // Check if it is linked to a suitable individual 96 foreach ($media->linkedIndividuals('OBJE') as $indi) { 97 if ( 98 $filter === 'all' || 99 $filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false || 100 $filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false 101 ) { 102 // Found one :-) 103 $random_media = $media; 104 break 2; 105 } 106 } 107 } 108 unset($all_media[$n]); 109 } 110 111 if ($random_media) { 112 $content = view('blocks/slide-show', [ 113 'block_id' => $block_id, 114 'media' => $random_media, 115 'media_file' => $media_file, 116 'show_controls' => $controls, 117 'start_automatically' => $start, 118 ]); 119 } else { 120 $content = I18N::translate('This family tree has no images to display.'); 121 } 122 123 if ($template) { 124 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 125 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 126 } else { 127 $config_url = ''; 128 } 129 130 return view('blocks/template', [ 131 'block' => str_replace('_', '-', $this->getName()), 132 'id' => $block_id, 133 'config_url' => $config_url, 134 'title' => $this->getTitle(), 135 'content' => $content, 136 ]); 137 } else { 138 return $content; 139 } 140 } 141 142 /** {@inheritdoc} */ 143 public function loadAjax(): bool { 144 return true; 145 } 146 147 /** {@inheritdoc} */ 148 public function isUserBlock(): bool { 149 return true; 150 } 151 152 /** {@inheritdoc} */ 153 public function isGedcomBlock(): bool { 154 return true; 155 } 156 157 /** 158 * An HTML form to edit block settings 159 * 160 * @param int $block_id 161 * 162 * @return void 163 */ 164 public function configureBlock($block_id) { 165 if (Filter::postBool('save') && Filter::checkCsrf()) { 166 $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 167 $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 168 $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 169 $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 170 $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 171 $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 172 $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 173 $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 174 $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 175 $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 176 $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 177 $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 178 $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 179 $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 180 $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 181 $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 182 $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 183 $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 184 $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 185 $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 186 $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 187 } 188 189 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 190 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 191 $start = $this->getBlockSetting($block_id, 'start', '0'); 192 193 $filters = [ 194 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 195 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 196 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 197 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 198 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 199 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 200 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 201 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 202 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 203 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 204 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 205 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 206 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 207 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 208 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 209 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 210 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 211 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 212 ]; 213 214 ?> 215 <div class="form-control row"> 216 <label class="col-sm-3 col-form-label" for="filter"> 217 <?= I18N::translate('Show only individuals, events, or all') ?> 218 </label> 219 <div class="col-sm-9"> 220 <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 221 </div> 222 </div> 223 224 <fieldset class="form-group"> 225 <div class="row"> 226 <legend class="col-form-label col-sm-3"> 227 <?= GedcomTag::getLabel('TYPE') ?> 228 </legend> 229 <div class="col-sm-9"> 230 <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 231 <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 232 <?php endforeach ?> 233 </div> 234 </div> 235 </fieldset> 236 237 <div class="form-group row"> 238 <label class="col-sm-3 col-form-label" for="controls"> 239 <?= I18N::translate('Show slide show controls') ?> 240 </label> 241 <div class="col-sm-9"> 242 <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 243 </div> 244 </div> 245 246 <div class="form-group row"> 247 <label class="col-sm-3 col-form-label" for="start"> 248 <?= I18N::translate('Start slide show on page load') ?> 249 </label> 250 <div class="col-sm-9"> 251 <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 252 </div> 253 </div> 254 <?php 255 } 256} 257