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\Database; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\GedcomTag; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Media; 24use Fisharebest\Webtrees\Tree; 25 26/** 27 * Class SlideShowModule 28 */ 29class SlideShowModule extends AbstractModule implements ModuleBlockInterface { 30 /** {@inheritdoc} */ 31 public function getTitle() { 32 return /* I18N: Name of a module */ I18N::translate('Slide show'); 33 } 34 35 /** {@inheritdoc} */ 36 public function getDescription() { 37 return /* I18N: Description of the “Slide show” module */ I18N::translate('Random images from the current family tree.'); 38 } 39 40 /** 41 * Generate the HTML content of this block. 42 * 43 * @param Tree $tree 44 * @param int $block_id 45 * @param bool $template 46 * @param string[] $cfg 47 * 48 * @return string 49 */ 50 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string { 51 global $ctype; 52 53 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 54 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 55 $start = $this->getBlockSetting($block_id, 'start', '0') || Filter::getBool('start'); 56 57 // We can apply the filters using SQL 58 // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 59 $all_media = Database::prepare( 60 "SELECT m_id FROM `##media`" . 61 " JOIN `##media_file` USING (m_file, m_id)" . 62 " WHERE m_file = ?" . 63 " AND multimedia_format IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 64 " AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" 65 )->execute([ 66 $tree->getTreeId(), 67 $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 68 $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 69 $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 70 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 71 $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 72 $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 73 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 74 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 75 $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 76 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 77 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 78 $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 79 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 80 $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 81 $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 82 $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 83 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 84 $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 85 ])->fetchOneColumn(); 86 87 // Keep looking through the media until a suitable one is found. 88 $random_media = null; 89 while ($all_media) { 90 $n = array_rand($all_media); 91 $media = Media::getInstance($all_media[$n], $tree); 92 $media_file = $media->firstImageFile(); 93 if ($media->canShow() && $media_file !== null && !$media_file->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('modules/random_media/slide-show', [ 112 'block_id' => $block_id, 113 'media' => $random_media, 114 'media_file' => $media_file, 115 'show_controls' => $controls, 116 'start_automatically' => $start, 117 'tree' => $tree, 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($tree)) { 125 $config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 126 } elseif ($ctype === 'user' && Auth::check()) { 127 $config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]); 128 } else { 129 $config_url = ''; 130 } 131 132 return view('modules/block-template', [ 133 'block' => str_replace('_', '-', $this->getName()), 134 'id' => $block_id, 135 'config_url' => $config_url, 136 'title' => $this->getTitle(), 137 'content' => $content, 138 ]); 139 } else { 140 return $content; 141 } 142 } 143 144 /** {@inheritdoc} */ 145 public function loadAjax(): bool { 146 return true; 147 } 148 149 /** {@inheritdoc} */ 150 public function isUserBlock(): bool { 151 return true; 152 } 153 154 /** {@inheritdoc} */ 155 public function isGedcomBlock(): bool { 156 return true; 157 } 158 159 /** 160 * An HTML form to edit block settings 161 * 162 * @param Tree $tree 163 * @param int $block_id 164 * 165 * @return void 166 */ 167 public function configureBlock(Tree $tree, int $block_id) { 168 if ($_SERVER['REQUEST_METHOD'] === 'POST') { 169 $this->setBlockSetting($block_id, 'filter', Filter::post('filter', 'indi|event|all', 'all')); 170 $this->setBlockSetting($block_id, 'controls', Filter::postBool('controls')); 171 $this->setBlockSetting($block_id, 'start', Filter::postBool('start')); 172 $this->setBlockSetting($block_id, 'filter_audio', Filter::postBool('filter_audio')); 173 $this->setBlockSetting($block_id, 'filter_book', Filter::postBool('filter_book')); 174 $this->setBlockSetting($block_id, 'filter_card', Filter::postBool('filter_card')); 175 $this->setBlockSetting($block_id, 'filter_certificate', Filter::postBool('filter_certificate')); 176 $this->setBlockSetting($block_id, 'filter_coat', Filter::postBool('filter_coat')); 177 $this->setBlockSetting($block_id, 'filter_document', Filter::postBool('filter_document')); 178 $this->setBlockSetting($block_id, 'filter_electronic', Filter::postBool('filter_electronic')); 179 $this->setBlockSetting($block_id, 'filter_fiche', Filter::postBool('filter_fiche')); 180 $this->setBlockSetting($block_id, 'filter_film', Filter::postBool('filter_film')); 181 $this->setBlockSetting($block_id, 'filter_magazine', Filter::postBool('filter_magazine')); 182 $this->setBlockSetting($block_id, 'filter_manuscript', Filter::postBool('filter_manuscript')); 183 $this->setBlockSetting($block_id, 'filter_map', Filter::postBool('filter_map')); 184 $this->setBlockSetting($block_id, 'filter_newspaper', Filter::postBool('filter_newspaper')); 185 $this->setBlockSetting($block_id, 'filter_other', Filter::postBool('filter_other')); 186 $this->setBlockSetting($block_id, 'filter_painting', Filter::postBool('filter_painting')); 187 $this->setBlockSetting($block_id, 'filter_photo', Filter::postBool('filter_photo')); 188 $this->setBlockSetting($block_id, 'filter_tombstone', Filter::postBool('filter_tombstone')); 189 $this->setBlockSetting($block_id, 'filter_video', Filter::postBool('filter_video')); 190 191 return; 192 } 193 194 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 195 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 196 $start = $this->getBlockSetting($block_id, 'start', '0'); 197 198 $filters = [ 199 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 200 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 201 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 202 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 203 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 204 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 205 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 206 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 207 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 208 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 209 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 210 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 211 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 212 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 213 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 214 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 215 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 216 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 217 ]; 218 219 $formats = GedcomTag::getFileFormTypes(); 220 221 echo view('modules/random_media/config', [ 222 'controls' => $controls, 223 'filter' => $filter, 224 'filters' => $filters, 225 'formats' => $formats, 226 'start' => $start, 227 ]); 228 } 229} 230