1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 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 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\GedcomTag; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Media; 24use Fisharebest\Webtrees\MediaFile; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Database\Query\JoinClause; 28use Illuminate\Support\Str; 29use Symfony\Component\HttpFoundation\Request; 30 31/** 32 * Class SlideShowModule 33 */ 34class SlideShowModule extends AbstractModule implements ModuleBlockInterface 35{ 36 use ModuleBlockTrait; 37 38 /** 39 * How should this module be identified in the control panel, etc.? 40 * 41 * @return string 42 */ 43 public function title(): string 44 { 45 /* I18N: Name of a module */ 46 return I18N::translate('Slide show'); 47 } 48 49 /** 50 * A sentence describing what this module does. 51 * 52 * @return string 53 */ 54 public function description(): string 55 { 56 /* I18N: Description of the “Slide show” module */ 57 return I18N::translate('Random images from the current family tree.'); 58 } 59 60 /** 61 * Generate the HTML content of this block. 62 * 63 * @param Tree $tree 64 * @param int $block_id 65 * @param string $ctype 66 * @param string[] $cfg 67 * 68 * @return string 69 */ 70 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 71 { 72 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 73 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 74 $start = (bool) $this->getBlockSetting($block_id, 'start', '0'); 75 76 $media_types = [ 77 $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 78 $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 79 $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 80 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 81 $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 82 $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 83 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 84 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 85 $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 86 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 87 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 88 $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 89 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 90 $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 91 $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 92 $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 93 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 94 $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 95 ]; 96 97 $media_types = array_filter($media_types); 98 99 // We can apply the filters using SQL 100 // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 101 $all_media = DB::table('media') 102 ->join('media_file', function (JoinClause $join): void { 103 $join 104 ->on('media_file.m_file', '=', 'media.m_file') 105 ->on('media_file.m_id', '=', 'media.m_id'); 106 }) 107 ->where('media.m_file', '=', $tree->id()) 108 ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp']) 109 ->whereIn('media_file.source_media_type', $media_types) 110 ->pluck('media.m_id') 111 ->all(); 112 113 // Keep looking through the media until a suitable one is found. 114 $random_media = null; 115 while (!empty($all_media)) { 116 $n = array_rand($all_media); 117 $media = Media::getInstance($all_media[$n], $tree); 118 $media_file = $media->firstImageFile(); 119 if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) { 120 // Check if it is linked to a suitable individual 121 foreach ($media->linkedIndividuals('OBJE') as $indi) { 122 if ( 123 $filter === 'all' || 124 $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false || 125 $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false 126 ) { 127 // Found one :-) 128 $random_media = $media; 129 break 2; 130 } 131 } 132 } 133 unset($all_media[$n]); 134 } 135 136 if ($random_media) { 137 $content = view('modules/random_media/slide-show', [ 138 'block_id' => $block_id, 139 'media' => $random_media, 140 'media_file' => $random_media->firstImageFile(), 141 'show_controls' => $controls, 142 'start_automatically' => $start, 143 'tree' => $tree, 144 ]); 145 } else { 146 $content = I18N::translate('This family tree has no images to display.'); 147 } 148 149 if ($ctype !== '') { 150 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 151 $config_url = route('tree-page-block-edit', [ 152 'block_id' => $block_id, 153 'ged' => $tree->name(), 154 ]); 155 } elseif ($ctype === 'user' && Auth::check()) { 156 $config_url = route('user-page-block-edit', [ 157 'block_id' => $block_id, 158 'ged' => $tree->name(), 159 ]); 160 } else { 161 $config_url = ''; 162 } 163 164 return view('modules/block-template', [ 165 'block' => Str::kebab($this->name()), 166 'id' => $block_id, 167 'config_url' => $config_url, 168 'title' => $this->title(), 169 'content' => $content, 170 ]); 171 } 172 173 return $content; 174 } 175 176 /** {@inheritdoc} */ 177 public function loadAjax(): bool 178 { 179 return true; 180 } 181 182 /** {@inheritdoc} */ 183 public function isUserBlock(): bool 184 { 185 return true; 186 } 187 188 /** {@inheritdoc} */ 189 public function isTreeBlock(): bool 190 { 191 return true; 192 } 193 194 /** 195 * Update the configuration for a block. 196 * 197 * @param Request $request 198 * @param int $block_id 199 * 200 * @return void 201 */ 202 public function saveBlockConfiguration(Request $request, int $block_id): void 203 { 204 $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all')); 205 $this->setBlockSetting($block_id, 'controls', $request->get('controls', '')); 206 $this->setBlockSetting($block_id, 'start', $request->get('start', '')); 207 $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', '')); 208 $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', '')); 209 $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', '')); 210 $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', '')); 211 $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', '')); 212 $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', '')); 213 $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', '')); 214 $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', '')); 215 $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', '')); 216 $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', '')); 217 $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', '')); 218 $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', '')); 219 $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', '')); 220 $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', '')); 221 $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', '')); 222 $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', '')); 223 $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', '')); 224 $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', '')); 225 } 226 227 /** 228 * An HTML form to edit block settings 229 * 230 * @param Tree $tree 231 * @param int $block_id 232 * 233 * @return void 234 */ 235 public function editBlockConfiguration(Tree $tree, int $block_id): void 236 { 237 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 238 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 239 $start = $this->getBlockSetting($block_id, 'start', '0'); 240 241 $filters = [ 242 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 243 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 244 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 245 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 246 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 247 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 248 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 249 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 250 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 251 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 252 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 253 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 254 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 255 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 256 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 257 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 258 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 259 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 260 ]; 261 262 $formats = GedcomTag::getFileFormTypes(); 263 264 echo view('modules/random_media/config', [ 265 'controls' => $controls, 266 'filter' => $filter, 267 'filters' => $filters, 268 'formats' => $formats, 269 'start' => $start, 270 ]); 271 } 272} 273