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\FontAwesome; 23use Fisharebest\Webtrees\Functions\FunctionsEdit; 24use Fisharebest\Webtrees\Functions\FunctionsPrint; 25use Fisharebest\Webtrees\GedcomTag; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Media; 28use Fisharebest\Webtrees\Theme; 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 $id = $this->getName() . $block_id; 112 $class = $this->getName() . '_block'; 113 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 114 $title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' '; 115 } else { 116 $title = ''; 117 } 118 $title .= $this->getTitle(); 119 120 if ($random_media) { 121 $content = '<div id="random_picture_container' . $block_id . '">'; 122 if ($controls) { 123 if ($start) { 124 $icon_class = 'icon-media-stop'; 125 } else { 126 $icon_class = 'icon-media-play'; 127 } 128 $content .= '<div dir="ltr" class="center" id="random_picture_controls' . $block_id . '"><br>'; 129 $content .= '<a href="#" onclick="togglePlay(); return false;" id="play_stop" class="' . $icon_class . '" title="' . I18N::translate('Play') . '/' . I18N::translate('Stop') . '"></a>'; 130 $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>'; 131 $content .= '</div><script> 132 var play = false; 133 function togglePlay() { 134 if (play) { 135 play = false; 136 $("#play_stop").removeClass("icon-media-stop").addClass("icon-media-play"); 137 } 138 else { 139 play = true; 140 playSlideShow(); 141 $("#play_stop").removeClass("icon-media-play").addClass("icon-media-stop"); 142 } 143 } 144 145 function playSlideShow() { 146 if (play) { 147 window.setTimeout("reload_image()", 6000); 148 } 149 } 150 function reload_image() { 151 if (play) { 152 $("#block_' . $block_id . '").load("index.php?ctype=' . $ctype . '&action=ajax&block_id=' . $block_id . '&start=1"); 153 } 154 } 155 </script>'; 156 } 157 if ($start) { 158 $content .= '<script>togglePlay();</script>'; 159 } 160 $content .= '<div class="center" id="random_picture_content' . $block_id . '">'; 161 $content .= '<table id="random_picture_box"><tr><td class="details1">'; 162 $content .= $random_media->displayImage(); 163 164 $content .= '<br>'; 165 $content .= '<a href="' . $random_media->getHtmlUrl() . '"><b>' . $random_media->getFullName() . '</b></a><br>'; 166 foreach ($random_media->linkedIndividuals('OBJE') as $individual) { 167 $content .= '<a href="' . $individual->getHtmlUrl() . '">' . I18N::translate('View this individual') . ' — ' . $individual->getFullName() . '</a><br>'; 168 } 169 foreach ($random_media->linkedFamilies('OBJE') as $family) { 170 $content .= '<a href="' . $family->getHtmlUrl() . '">' . I18N::translate('View this family') . ' — ' . $family->getFullName() . '</a><br>'; 171 } 172 foreach ($random_media->linkedSources('OBJE') as $source) { 173 $content .= '<a href="' . $source->getHtmlUrl() . '">' . I18N::translate('View this source') . ' — ' . $source->getFullName() . '</a><br>'; 174 } 175 $content .= '<br><div class="indent">'; 176 $content .= FunctionsPrint::printFactNotes($random_media->getGedcom(), '1', false); 177 $content .= '</div>'; 178 $content .= '</td></tr></table>'; 179 $content .= '</div>'; // random_picture_content 180 $content .= '</div>'; // random_picture_container 181 } else { 182 $content = I18N::translate('This family tree has no images to display.'); 183 } 184 if ($template) { 185 return Theme::theme()->formatBlock($id, $title, $class, $content); 186 } else { 187 return $content; 188 } 189 } 190 191 /** {@inheritdoc} */ 192 public function loadAjax() { 193 return true; 194 } 195 196 /** {@inheritdoc} */ 197 public function isUserBlock() { 198 return true; 199 } 200 201 /** {@inheritdoc} */ 202 public function isGedcomBlock() { 203 return true; 204 } 205 206 /** 207 * An HTML form to edit block settings 208 * 209 * @param int $block_id 210 */ 211 public function configureBlock($block_id) { 212 if (Filter::postBool('save') && Filter::checkCsrf()) { 213 $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 214 $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 215 $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 216 $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 217 $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 218 $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 219 $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 220 $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 221 $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 222 $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 223 $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 224 $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 225 $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 226 $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 227 $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 228 $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 229 $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 230 $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 231 $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 232 $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 233 $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 234 } 235 236 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 237 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 238 $start = $this->getBlockSetting($block_id, 'start', '0'); 239 240 $filters = [ 241 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 242 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 243 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 244 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 245 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 246 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 247 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 248 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 249 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 250 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 251 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 252 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 253 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 254 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 255 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 256 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 257 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 258 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 259 ]; 260 261 ?> 262 <div class="form-control row"> 263 <label class="col-sm-3 col-form-label" for="filter"> 264 <?= I18N::translate('Show only individuals, events, or all') ?> 265 </label> 266 <div class="col-sm-9"> 267 <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 268 </div> 269 </div> 270 271 <fieldset class="form-group"> 272 <div class="row"> 273 <legend class="col-form-legend col-sm-3"> 274 <?= GedcomTag::getLabel('TYPE') ?> 275 </legend> 276 <div class="col-sm-9"> 277 <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 278 <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 279 <?php endforeach ?> 280 </div> 281 </div> 282 </fieldset> 283 284 <div class="form-group row"> 285 <label class="col-sm-3 col-form-label" for="controls"> 286 <?= I18N::translate('Show slide show controls') ?> 287 </label> 288 <div class="col-sm-9"> 289 <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 290 </div> 291 </div> 292 293 <div class="form-group row"> 294 <label class="col-sm-3 col-form-label" for="start"> 295 <?= I18N::translate('Start slide show on page load') ?> 296 </label> 297 <div class="col-sm-9"> 298 <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 299 </div> 300 </div> 301 <?php 302 } 303} 304