1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 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\Functions\FunctionsPrint; 24use Fisharebest\Webtrees\GedcomTag; 25use Fisharebest\Webtrees\Html; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Media; 28use Fisharebest\Webtrees\View; 29 30/** 31 * Class SlideShowModule 32 */ 33class SlideShowModule extends AbstractModule implements ModuleBlockInterface { 34 /** {@inheritdoc} */ 35 public function getTitle() { 36 return /* I18N: Name of a module */ I18N::translate('Slide show'); 37 } 38 39 /** {@inheritdoc} */ 40 public function getDescription() { 41 return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.'); 42 } 43 44 /** 45 * Generate the HTML content of this block. 46 * 47 * @param int $block_id 48 * @param bool $template 49 * @param string[] $cfg 50 * 51 * @return string 52 */ 53 public function getBlock($block_id, $template = true, $cfg = []) { 54 global $ctype, $WT_TREE; 55 56 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 57 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 58 $start = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start'); 59 60 // We can apply the filters using SQL 61 // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 62 $all_media = Database::prepare( 63 "SELECT m_id FROM `##media`" . 64 " WHERE m_file = ?" . 65 " AND m_ext IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 66 " AND m_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" 67 )->execute([ 68 $WT_TREE->getTreeId(), 69 $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 70 $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 71 $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 72 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 73 $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 74 $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 75 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 76 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 77 $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 78 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 79 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 80 $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 81 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 82 $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 83 $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 84 $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 85 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 86 $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 87 ])->fetchOneColumn(); 88 89 // Keep looking through the media until a suitable one is found. 90 $random_media = null; 91 while ($all_media) { 92 $n = array_rand($all_media); 93 $media = Media::getInstance($all_media[$n], $WT_TREE); 94 if ($media->canShow() && !$media->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 = '<div id="random_picture_container' . $block_id . '">'; 113 if ($controls) { 114 if ($start) { 115 $icon_class = 'icon-media-stop'; 116 } else { 117 $icon_class = 'icon-media-play'; 118 } 119 $content .= '<div dir="ltr" class="center" id="random_picture_controls' . $block_id . '"><br>'; 120 $content .= '<a href="#" onclick="togglePlay(); return false;" id="play_stop" class="' . $icon_class . '" title="' . I18N::translate('Play') . '/' . I18N::translate('Stop') . '"></a>'; 121 $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>'; 122 $content .= '</div><script> 123 var play = false; 124 function togglePlay() { 125 if (play) { 126 play = false; 127 $("#play_stop").removeClass("icon-media-stop").addClass("icon-media-play"); 128 } 129 else { 130 play = true; 131 playSlideShow(); 132 $("#play_stop").removeClass("icon-media-play").addClass("icon-media-stop"); 133 } 134 } 135 136 function playSlideShow() { 137 if (play) { 138 window.setTimeout("reload_image()", 6000); 139 } 140 } 141 function reload_image() { 142 if (play) { 143 $("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '&start=1"); 144 } 145 } 146 </script>'; 147 } 148 if ($start) { 149 $content .= '<script>togglePlay();</script>'; 150 } 151 $content .= '<div class="center" id="random_picture_content' . $block_id . '">'; 152 $content .= '<table id="random_picture_box"><tr><td class="details1">'; 153 $content .= $random_media->displayImage(200, 200, '', []); 154 155 $content .= '<br>'; 156 $content .= '<a href="' . $random_media->getHtmlUrl() . '"><b>' . $random_media->getFullName() . '</b></a><br>'; 157 foreach ($random_media->linkedIndividuals('OBJE') as $individual) { 158 $content .= '<a href="' . $individual->getHtmlUrl() . '">' . I18N::translate('View this individual') . ' — ' . $individual->getFullName() . '</a><br>'; 159 } 160 foreach ($random_media->linkedFamilies('OBJE') as $family) { 161 $content .= '<a href="' . $family->getHtmlUrl() . '">' . I18N::translate('View this family') . ' — ' . $family->getFullName() . '</a><br>'; 162 } 163 foreach ($random_media->linkedSources('OBJE') as $source) { 164 $content .= '<a href="' . $source->getHtmlUrl() . '">' . I18N::translate('View this source') . ' — ' . $source->getFullName() . '</a><br>'; 165 } 166 $content .= '<br><div class="indent">'; 167 $content .= FunctionsPrint::printFactNotes($random_media->getGedcom(), '1', false); 168 $content .= '</div>'; 169 $content .= '</td></tr></table>'; 170 $content .= '</div>'; // random_picture_content 171 $content .= '</div>'; // random_picture_container 172 } else { 173 $content = I18N::translate('This family tree has no images to display.'); 174 } 175 if ($template) { 176 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 177 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 178 } else { 179 $config_url = ''; 180 } 181 182 return View::make('blocks/template', [ 183 'block' => str_replace('_', '-', $this->getName()), 184 'id' => $block_id, 185 'config_url' => $config_url, 186 'title' => $this->getTitle(), 187 'content' => $content, 188 ]); 189 } else { 190 return $content; 191 } 192 } 193 194 /** {@inheritdoc} */ 195 public function loadAjax() { 196 return true; 197 } 198 199 /** {@inheritdoc} */ 200 public function isUserBlock() { 201 return true; 202 } 203 204 /** {@inheritdoc} */ 205 public function isGedcomBlock() { 206 return true; 207 } 208 209 /** 210 * An HTML form to edit block settings 211 * 212 * @param int $block_id 213 */ 214 public function configureBlock($block_id) { 215 if (Filter::postBool('save') && Filter::checkCsrf()) { 216 $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 217 $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 218 $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 219 $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 220 $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 221 $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 222 $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 223 $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 224 $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 225 $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 226 $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 227 $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 228 $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 229 $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 230 $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 231 $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 232 $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 233 $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 234 $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 235 $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 236 $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 237 } 238 239 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 240 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 241 $start = $this->getBlockSetting($block_id, 'start', '0'); 242 243 $filters = [ 244 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 245 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 246 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 247 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 248 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 249 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 250 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 251 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 252 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 253 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 254 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 255 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 256 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 257 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 258 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 259 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 260 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 261 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 262 ]; 263 264 ?> 265 <div class="form-control row"> 266 <label class="col-sm-3 col-form-label" for="filter"> 267 <?= I18N::translate('Show only individuals, events, or all') ?> 268 </label> 269 <div class="col-sm-9"> 270 <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 271 </div> 272 </div> 273 274 <fieldset class="form-group"> 275 <div class="row"> 276 <legend class="col-form-legend col-sm-3"> 277 <?= GedcomTag::getLabel('TYPE') ?> 278 </legend> 279 <div class="col-sm-9"> 280 <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 281 <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 282 <?php endforeach ?> 283 </div> 284 </div> 285 </fieldset> 286 287 <div class="form-group row"> 288 <label class="col-sm-3 col-form-label" for="controls"> 289 <?= I18N::translate('Show slide show controls') ?> 290 </label> 291 <div class="col-sm-9"> 292 <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 293 </div> 294 </div> 295 296 <div class="form-group row"> 297 <label class="col-sm-3 col-form-label" for="start"> 298 <?= I18N::translate('Start slide show on page load') ?> 299 </label> 300 <div class="col-sm-9"> 301 <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 302 </div> 303 </div> 304 <?php 305 } 306} 307