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\GedcomTag; 24use Fisharebest\Webtrees\Html; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Media; 27use Fisharebest\Webtrees\View; 28 29/** 30 * Class SlideShowModule 31 */ 32class SlideShowModule extends AbstractModule implements ModuleBlockInterface { 33 /** {@inheritdoc} */ 34 public function getTitle() { 35 return /* I18N: Name of a module */ I18N::translate('Slide show'); 36 } 37 38 /** {@inheritdoc} */ 39 public function getDescription() { 40 return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.'); 41 } 42 43 /** 44 * Generate the HTML content of this block. 45 * 46 * @param int $block_id 47 * @param bool $template 48 * @param string[] $cfg 49 * 50 * @return string 51 */ 52 public function getBlock($block_id, $template = true, $cfg = []) { 53 global $ctype, $WT_TREE; 54 55 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 56 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 57 $start = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start'); 58 59 // We can apply the filters using SQL 60 // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 61 $all_media = Database::prepare( 62 "SELECT m_id FROM `##media`" . 63 " WHERE m_file = ?" . 64 " AND m_ext IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 65 " AND m_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 if ($media->canShow() && !$media->isExternal()) { 94 // Check if it is linked to a suitable individual 95 foreach ($media->linkedIndividuals('OBJE') as $indi) { 96 if ( 97 $filter === 'all' || 98 $filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false || 99 $filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false 100 ) { 101 // Found one :-) 102 $random_media = $media; 103 break 2; 104 } 105 } 106 } 107 unset($all_media[$n]); 108 } 109 110 if ($random_media) { 111 $content = View::make('blocks/slide-show', [ 112 'block_id' => $block_id, 113 'media' => $random_media, 114 'show_controls' => $controls, 115 'start_automatically' => $start, 116 ]); 117 } else { 118 $content = I18N::translate('This family tree has no images to display.'); 119 } 120 121 if ($template) { 122 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 123 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 124 } else { 125 $config_url = ''; 126 } 127 128 return View::make('blocks/template', [ 129 'block' => str_replace('_', '-', $this->getName()), 130 'id' => $block_id, 131 'config_url' => $config_url, 132 'title' => $this->getTitle(), 133 'content' => $content, 134 ]); 135 } else { 136 return $content; 137 } 138 } 139 140 /** {@inheritdoc} */ 141 public function loadAjax() { 142 return true; 143 } 144 145 /** {@inheritdoc} */ 146 public function isUserBlock() { 147 return true; 148 } 149 150 /** {@inheritdoc} */ 151 public function isGedcomBlock() { 152 return true; 153 } 154 155 /** 156 * An HTML form to edit block settings 157 * 158 * @param int $block_id 159 */ 160 public function configureBlock($block_id) { 161 if (Filter::postBool('save') && Filter::checkCsrf()) { 162 $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 163 $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 164 $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 165 $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 166 $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 167 $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 168 $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 169 $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 170 $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 171 $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 172 $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 173 $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 174 $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 175 $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 176 $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 177 $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 178 $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 179 $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 180 $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 181 $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 182 $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 183 } 184 185 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 186 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 187 $start = $this->getBlockSetting($block_id, 'start', '0'); 188 189 $filters = [ 190 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 191 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 192 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 193 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 194 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 195 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 196 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 197 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 198 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 199 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 200 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 201 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 202 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 203 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 204 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 205 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 206 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 207 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 208 ]; 209 210 ?> 211 <div class="form-control row"> 212 <label class="col-sm-3 col-form-label" for="filter"> 213 <?= I18N::translate('Show only individuals, events, or all') ?> 214 </label> 215 <div class="col-sm-9"> 216 <?= Bootstrap4::select(['indi' => I18N::translate('Individuals'), 'event' => I18N::translate('Facts and events'), 'all' => I18N::translate('All')], $filter, ['id' => 'filter', 'name' => 'filter']) ?> 217 </div> 218 </div> 219 220 <fieldset class="form-group"> 221 <div class="row"> 222 <legend class="col-form-legend col-sm-3"> 223 <?= GedcomTag::getLabel('TYPE') ?> 224 </legend> 225 <div class="col-sm-9"> 226 <?php foreach (GedcomTag::getFileFormTypes() as $typeName => $typeValue): ?> 227 <?= Bootstrap4::checkbox($typeValue, false, ['name' => 'filter_' . $typeName, 'checked' => (bool) $filters[$typeName]]) ?> 228 <?php endforeach ?> 229 </div> 230 </div> 231 </fieldset> 232 233 <div class="form-group row"> 234 <label class="col-sm-3 col-form-label" for="controls"> 235 <?= I18N::translate('Show slide show controls') ?> 236 </label> 237 <div class="col-sm-9"> 238 <?= Bootstrap4::radioButtons('controls', FunctionsEdit::optionsNoYes(), $controls, true) ?> 239 </div> 240 </div> 241 242 <div class="form-group row"> 243 <label class="col-sm-3 col-form-label" for="start"> 244 <?= I18N::translate('Start slide show on page load') ?> 245 </label> 246 <div class="col-sm-9"> 247 <?= Bootstrap4::radioButtons('start', FunctionsEdit::optionsNoYes(), $start, true) ?> 248 </div> 249 </div> 250 <?php 251 } 252} 253