1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Media; 24use Fisharebest\Webtrees\Registry; 25use Fisharebest\Webtrees\Services\LinkedRecordService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Database\Capsule\Manager as DB; 28use Illuminate\Database\Query\JoinClause; 29use Illuminate\Support\Str; 30use Psr\Http\Message\ServerRequestInterface; 31 32use function app; 33use function array_filter; 34use function assert; 35use function in_array; 36use function str_contains; 37 38/** 39 * Class SlideShowModule 40 */ 41class SlideShowModule extends AbstractModule implements ModuleBlockInterface 42{ 43 use ModuleBlockTrait; 44 45 // Show media linked to events or individuals. 46 private const LINK_ALL = 'all'; 47 private const LINK_EVENT = 'event'; 48 private const LINK_INDIVIDUAL = 'indi'; 49 50 // How long to show each slide (seconds) 51 private const DELAY = 6; 52 53 private LinkedRecordService $linked_record_service; 54 55 /** 56 * @param LinkedRecordService $linked_record_service 57 */ 58 public function __construct(LinkedRecordService $linked_record_service) 59 { 60 $this->linked_record_service = $linked_record_service; 61 } 62 63 /** 64 * A sentence describing what this module does. 65 * 66 * @return string 67 */ 68 public function description(): string 69 { 70 /* I18N: Description of the “Slide show” module */ 71 return I18N::translate('Random images from the current family tree.'); 72 } 73 74 /** 75 * Generate the HTML content of this block. 76 * 77 * @param Tree $tree 78 * @param int $block_id 79 * @param string $context 80 * @param array<string,string> $config 81 * 82 * @return string 83 */ 84 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 85 { 86 $request = app(ServerRequestInterface::class); 87 $default_start = $this->getBlockSetting($block_id, 'start'); 88 $filter_links = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); 89 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 90 $start = (bool) ($request->getQueryParams()['start'] ?? $default_start); 91 92 $filter_types = [ 93 $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 94 $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 95 $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 96 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 97 $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 98 $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 99 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 100 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 101 $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 102 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 103 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 104 $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 105 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 106 $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 107 $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 108 $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 109 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 110 $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 111 ]; 112 113 $filter_types = array_filter($filter_types); 114 115 // The type "other" includes media without a type. 116 if (in_array('other', $filter_types, true)) { 117 $filter_types[] = ''; 118 } 119 120 // We can apply the filters using SQL, but it is more efficient to shuffle in PHP. 121 $random_row = DB::table('media') 122 ->join('media_file', static function (JoinClause $join): void { 123 $join 124 ->on('media_file.m_file', '=', 'media.m_file') 125 ->on('media_file.m_id', '=', 'media.m_id'); 126 }) 127 ->where('media.m_file', '=', $tree->id()) 128 ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp']) 129 ->whereIn('media_file.source_media_type', $filter_types) 130 ->select('media.*') 131 ->get() 132 ->shuffle() 133 ->first(function (object $row) use ($filter_links, $tree): bool { 134 $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom); 135 assert($media instanceof Media); 136 137 if (!$media->canShow() || $media->firstImageFile() === null) { 138 return false; 139 } 140 141 foreach ($this->linked_record_service->linkedIndividuals($media) as $individual) { 142 switch ($filter_links) { 143 case self::LINK_ALL: 144 return true; 145 146 case self::LINK_INDIVIDUAL: 147 return str_contains($individual->gedcom(), "\n1 OBJE @" . $media->xref() . '@'); 148 149 case self::LINK_EVENT: 150 return str_contains($individual->gedcom(), "\n2 OBJE @" . $media->xref() . '@'); 151 } 152 } 153 154 return false; 155 }); 156 157 $random_media = null; 158 159 if ($random_row !== null) { 160 $random_media = Registry::mediaFactory()->make($random_row->m_id, $tree, $random_row->m_gedcom); 161 } 162 163 if ($random_media instanceof Media) { 164 $content = view('modules/random_media/slide-show', [ 165 'block_id' => $block_id, 166 'delay' => self::DELAY, 167 'linked_families' => $this->linked_record_service->linkedFamilies($random_media), 168 'linked_individuals' => $this->linked_record_service->linkedIndividuals($random_media), 169 'linked_sources' => $this->linked_record_service->linkedSources($random_media), 170 'media' => $random_media, 171 'media_file' => $random_media->firstImageFile(), 172 'show_controls' => $controls, 173 'start_automatically' => $start, 174 'tree' => $tree, 175 ]); 176 } else { 177 $content = I18N::translate('This family tree has no images to display.'); 178 } 179 180 if ($context !== self::CONTEXT_EMBED) { 181 return view('modules/block-template', [ 182 'block' => Str::kebab($this->name()), 183 'id' => $block_id, 184 'config_url' => $this->configUrl($tree, $context, $block_id), 185 'title' => $this->title(), 186 'content' => $content, 187 ]); 188 } 189 190 return $content; 191 } 192 193 /** 194 * How should this module be identified in the control panel, etc.? 195 * 196 * @return string 197 */ 198 public function title(): string 199 { 200 /* I18N: Name of a module */ 201 return I18N::translate('Slide show'); 202 } 203 204 /** 205 * Should this block load asynchronously using AJAX? 206 * 207 * Simple blocks are faster in-line, more complex ones can be loaded later. 208 * 209 * @return bool 210 */ 211 public function loadAjax(): bool 212 { 213 return true; 214 } 215 216 /** 217 * Can this block be shown on the user’s home page? 218 * 219 * @return bool 220 */ 221 public function isUserBlock(): bool 222 { 223 return true; 224 } 225 226 /** 227 * Can this block be shown on the tree’s home page? 228 * 229 * @return bool 230 */ 231 public function isTreeBlock(): bool 232 { 233 return true; 234 } 235 236 /** 237 * Update the configuration for a block. 238 * 239 * @param ServerRequestInterface $request 240 * @param int $block_id 241 * 242 * @return void 243 */ 244 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 245 { 246 $params = (array) $request->getParsedBody(); 247 248 $this->setBlockSetting($block_id, 'filter', $params['filter']); 249 $this->setBlockSetting($block_id, 'controls', $params['controls']); 250 $this->setBlockSetting($block_id, 'start', $params['start']); 251 $this->setBlockSetting($block_id, 'filter_audio', $params['filter_audio'] ?? ''); 252 $this->setBlockSetting($block_id, 'filter_book', $params['filter_book'] ?? ''); 253 $this->setBlockSetting($block_id, 'filter_card', $params['filter_card'] ?? ''); 254 $this->setBlockSetting($block_id, 'filter_certificate', $params['filter_certificate'] ?? ''); 255 $this->setBlockSetting($block_id, 'filter_coat', $params['filter_coat'] ?? ''); 256 $this->setBlockSetting($block_id, 'filter_document', $params['filter_document'] ?? ''); 257 $this->setBlockSetting($block_id, 'filter_electronic', $params['filter_electronic'] ?? ''); 258 $this->setBlockSetting($block_id, 'filter_fiche', $params['filter_fiche'] ?? ''); 259 $this->setBlockSetting($block_id, 'filter_film', $params['filter_film'] ?? ''); 260 $this->setBlockSetting($block_id, 'filter_magazine', $params['filter_magazine'] ?? ''); 261 $this->setBlockSetting($block_id, 'filter_manuscript', $params['filter_manuscript'] ?? ''); 262 $this->setBlockSetting($block_id, 'filter_map', $params['filter_map'] ?? ''); 263 $this->setBlockSetting($block_id, 'filter_newspaper', $params['filter_newspaper'] ?? ''); 264 $this->setBlockSetting($block_id, 'filter_other', $params['filter_other'] ?? ''); 265 $this->setBlockSetting($block_id, 'filter_painting', $params['filter_painting'] ?? ''); 266 $this->setBlockSetting($block_id, 'filter_photo', $params['filter_photo'] ?? ''); 267 $this->setBlockSetting($block_id, 'filter_tombstone', $params['filter_tombstone'] ?? ''); 268 $this->setBlockSetting($block_id, 'filter_video', $params['filter_video'] ?? ''); 269 } 270 271 /** 272 * An HTML form to edit block settings 273 * 274 * @param Tree $tree 275 * @param int $block_id 276 * 277 * @return string 278 */ 279 public function editBlockConfiguration(Tree $tree, int $block_id): string 280 { 281 $filter = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); 282 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 283 $start = $this->getBlockSetting($block_id, 'start', '0'); 284 285 $filters = [ 286 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 287 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 288 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 289 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 290 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 291 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 292 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 293 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 294 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 295 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 296 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 297 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 298 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 299 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 300 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 301 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 302 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 303 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 304 ]; 305 306 $formats = array_filter(Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values()); 307 308 return view('modules/random_media/config', [ 309 'controls' => $controls, 310 'filter' => $filter, 311 'filters' => $filters, 312 'formats' => $formats, 313 'start' => $start, 314 ]); 315 } 316} 317