1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\Elements\SourceMediaType; 23use Fisharebest\Webtrees\Factories\ImageFactory; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Media; 26use Fisharebest\Webtrees\Registry; 27use Fisharebest\Webtrees\Services\LinkedRecordService; 28use Fisharebest\Webtrees\Tree; 29use Illuminate\Database\Capsule\Manager as DB; 30use Illuminate\Database\Query\JoinClause; 31use Illuminate\Support\Str; 32use Psr\Http\Message\ServerRequestInterface; 33 34use function app; 35use function array_filter; 36use function in_array; 37use function str_contains; 38 39/** 40 * Class SlideShowModule 41 */ 42class SlideShowModule extends AbstractModule implements ModuleBlockInterface 43{ 44 use ModuleBlockTrait; 45 46 // Show media linked to events or individuals. 47 private const LINK_ALL = 'all'; 48 private const LINK_EVENT = 'event'; 49 private const LINK_INDIVIDUAL = 'indi'; 50 51 // How long to show each slide (seconds) 52 private const DELAY = 6; 53 54 private LinkedRecordService $linked_record_service; 55 56 /** 57 * @param LinkedRecordService $linked_record_service 58 */ 59 public function __construct(LinkedRecordService $linked_record_service) 60 { 61 $this->linked_record_service = $linked_record_service; 62 } 63 64 /** 65 * A sentence describing what this module does. 66 * 67 * @return string 68 */ 69 public function description(): string 70 { 71 /* I18N: Description of the “Slide show” module */ 72 return I18N::translate('Random images from the current family tree.'); 73 } 74 75 /** 76 * Generate the HTML content of this block. 77 * 78 * @param Tree $tree 79 * @param int $block_id 80 * @param string $context 81 * @param array<string,string> $config 82 * 83 * @return string 84 */ 85 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 86 { 87 $request = app(ServerRequestInterface::class); 88 $default_start = $this->getBlockSetting($block_id, 'start'); 89 $filter_links = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); 90 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 91 $start = (bool) ($request->getQueryParams()['start'] ?? $default_start); 92 93 $filter_types = [ 94 $this->getBlockSetting($block_id, 'filter_audio', '0') ? SourceMediaType::TYPE_AUDIO : null, 95 $this->getBlockSetting($block_id, 'filter_book', '1') ? SourceMediaType::TYPE_BOOK : null, 96 $this->getBlockSetting($block_id, 'filter_card', '1') ? SourceMediaType::TYPE_CARD : null, 97 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? SourceMediaType::TYPE_CERTIFICATE : null, 98 $this->getBlockSetting($block_id, 'filter_coat', '1') ? SourceMediaType::TYPE_COAT : null, 99 $this->getBlockSetting($block_id, 'filter_document', '1') ? SourceMediaType::TYPE_DOCUMENT : null, 100 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? SourceMediaType::TYPE_ELECTRONIC : null, 101 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? SourceMediaType::TYPE_FICHE : null, 102 $this->getBlockSetting($block_id, 'filter_film', '1') ? SourceMediaType::TYPE_FILM : null, 103 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? SourceMediaType::TYPE_MAGAZINE : null, 104 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? SourceMediaType::TYPE_MANUSCRIPT : null, 105 $this->getBlockSetting($block_id, 'filter_map', '1') ? SourceMediaType::TYPE_MAP : null, 106 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? SourceMediaType::TYPE_NEWSPAPER : null, 107 $this->getBlockSetting($block_id, 'filter_other', '1') ? SourceMediaType::TYPE_OTHER : null, 108 $this->getBlockSetting($block_id, 'filter_painting', '1') ? SourceMediaType::TYPE_PAINTING : null, 109 $this->getBlockSetting($block_id, 'filter_photo', '1') ? SourceMediaType::TYPE_PHOTO : null, 110 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? SourceMediaType::TYPE_TOMBSTONE : null, 111 $this->getBlockSetting($block_id, 'filter_video', '0') ? SourceMediaType::TYPE_VIDEO : null, 112 ]; 113 114 $filter_types = array_filter($filter_types); 115 116 // The type "other" includes media without a type. 117 if (in_array('other', $filter_types, true)) { 118 $filter_types[] = ''; 119 } 120 121 // We can apply the filters using SQL, but it is more efficient to shuffle in PHP. 122 $random_row = DB::table('media') 123 ->join('media_file', static function (JoinClause $join): void { 124 $join 125 ->on('media_file.m_file', '=', 'media.m_file') 126 ->on('media_file.m_id', '=', 'media.m_id'); 127 }) 128 ->where('media.m_file', '=', $tree->id()) 129 ->whereIn('media_file.multimedia_format', ImageFactory::SUPPORTED_FORMATS) 130 ->whereIn('media_file.source_media_type', $filter_types) 131 ->select('media.*') 132 ->get() 133 ->shuffle() 134 ->first(function (object $row) use ($filter_links, $tree): bool { 135 $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom); 136 137 if ($media === null || !$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 SourceMediaType::TYPE_AUDIO => $this->getBlockSetting($block_id, 'filter_audio', '0'), 287 SourceMediaType::TYPE_BOOK => $this->getBlockSetting($block_id, 'filter_book', '1'), 288 SourceMediaType::TYPE_CARD => $this->getBlockSetting($block_id, 'filter_card', '1'), 289 SourceMediaType::TYPE_CERTIFICATE => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 290 SourceMediaType::TYPE_COAT => $this->getBlockSetting($block_id, 'filter_coat', '1'), 291 SourceMediaType::TYPE_DOCUMENT => $this->getBlockSetting($block_id, 'filter_document', '1'), 292 SourceMediaType::TYPE_ELECTRONIC => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 293 SourceMediaType::TYPE_FICHE => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 294 SourceMediaType::TYPE_FILM => $this->getBlockSetting($block_id, 'filter_film', '1'), 295 SourceMediaType::TYPE_MAGAZINE => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 296 SourceMediaType::TYPE_MANUSCRIPT => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 297 SourceMediaType::TYPE_MAP => $this->getBlockSetting($block_id, 'filter_map', '1'), 298 SourceMediaType::TYPE_NEWSPAPER => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 299 SourceMediaType::TYPE_OTHER => $this->getBlockSetting($block_id, 'filter_other', '1'), 300 SourceMediaType::TYPE_PAINTING => $this->getBlockSetting($block_id, 'filter_painting', '1'), 301 SourceMediaType::TYPE_PHOTO => $this->getBlockSetting($block_id, 'filter_photo', '1'), 302 SourceMediaType::TYPE_TOMBSTONE => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 303 SourceMediaType::TYPE_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