1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\DB; 23use Fisharebest\Webtrees\Elements\SourceMediaType; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Media; 26use Fisharebest\Webtrees\Registry; 27use Fisharebest\Webtrees\Services\LinkedRecordService; 28use Fisharebest\Webtrees\Tree; 29use Fisharebest\Webtrees\Validator; 30use Illuminate\Database\Query\JoinClause; 31use Illuminate\Support\Str; 32use Psr\Http\Message\ServerRequestInterface; 33 34use function array_filter; 35use function in_array; 36use function str_contains; 37use function strtolower; 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 // New data is normalized. Old data may contain jpg/jpeg, tif/tiff. 55 private const SUPPORTED_FORMATS = ['bmp', 'gif', 'jpeg', 'jpg', 'png', 'tif', 'tiff', 'webp']; 56 57 private LinkedRecordService $linked_record_service; 58 59 /** 60 * @param LinkedRecordService $linked_record_service 61 */ 62 public function __construct(LinkedRecordService $linked_record_service) 63 { 64 $this->linked_record_service = $linked_record_service; 65 } 66 67 public function description(): string 68 { 69 /* I18N: Description of the “Slide show” module */ 70 return I18N::translate('Random images from the current family tree.'); 71 } 72 73 /** 74 * Generate the HTML content of this block. 75 * 76 * @param Tree $tree 77 * @param int $block_id 78 * @param string $context 79 * @param array<string,string> $config 80 * 81 * @return string 82 */ 83 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 84 { 85 $request = Registry::container()->get(ServerRequestInterface::class); 86 $default_start = (bool) $this->getBlockSetting($block_id, 'start'); 87 $filter_links = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); 88 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 89 $start = Validator::queryParams($request)->boolean('start', $default_start); 90 91 $filter_types = [ 92 $this->getBlockSetting($block_id, 'filter_audio', '0') ? SourceMediaType::VALUE_AUDIO : null, 93 $this->getBlockSetting($block_id, 'filter_book', '1') ? SourceMediaType::VALUE_BOOK : null, 94 $this->getBlockSetting($block_id, 'filter_card', '1') ? SourceMediaType::VALUE_CARD : null, 95 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? SourceMediaType::VALUE_CERTIFICATE : null, 96 $this->getBlockSetting($block_id, 'filter_coat', '1') ? SourceMediaType::VALUE_COAT : null, 97 $this->getBlockSetting($block_id, 'filter_document', '1') ? SourceMediaType::VALUE_DOCUMENT : null, 98 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? SourceMediaType::VALUE_ELECTRONIC : null, 99 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? SourceMediaType::VALUE_FICHE : null, 100 $this->getBlockSetting($block_id, 'filter_film', '1') ? SourceMediaType::VALUE_FILM : null, 101 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? SourceMediaType::VALUE_MAGAZINE : null, 102 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? SourceMediaType::VALUE_MANUSCRIPT : null, 103 $this->getBlockSetting($block_id, 'filter_map', '1') ? SourceMediaType::VALUE_MAP : null, 104 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? SourceMediaType::VALUE_NEWSPAPER : null, 105 $this->getBlockSetting($block_id, 'filter_other', '1') ? SourceMediaType::VALUE_OTHER : null, 106 $this->getBlockSetting($block_id, 'filter_painting', '1') ? SourceMediaType::VALUE_PAINTING : null, 107 $this->getBlockSetting($block_id, 'filter_photo', '1') ? SourceMediaType::VALUE_PHOTO : null, 108 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? SourceMediaType::VALUE_TOMBSTONE : null, 109 $this->getBlockSetting($block_id, 'filter_video', '0') ? SourceMediaType::VALUE_VIDEO : null, 110 ]; 111 112 $filter_types = array_filter($filter_types); 113 114 // The type "other" includes media without a type. 115 if (in_array(SourceMediaType::VALUE_OTHER, $filter_types, true)) { 116 $filter_types[] = ''; 117 } 118 119 // We can apply the filters using SQL, but it is more efficient to shuffle in PHP. 120 $random_row = DB::table('media') 121 ->join('media_file', static function (JoinClause $join): void { 122 $join 123 ->on('media_file.m_file', '=', 'media.m_file') 124 ->on('media_file.m_id', '=', 'media.m_id'); 125 }) 126 ->where('media.m_file', '=', $tree->id()) 127 ->whereIn('media_file.multimedia_format', self::SUPPORTED_FORMATS) 128 ->whereIn('media_file.source_media_type', $filter_types) 129 ->select(['media.*']) 130 ->get() 131 ->shuffle() 132 ->first(function (object $row) use ($filter_links, $tree): bool { 133 $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom); 134 135 if ($media === null || !$media->canShow() || $media->firstImageFile() === null) { 136 return false; 137 } 138 139 foreach ($this->linked_record_service->linkedIndividuals($media) as $individual) { 140 switch ($filter_links) { 141 case self::LINK_ALL: 142 return true; 143 144 case self::LINK_INDIVIDUAL: 145 return str_contains($individual->gedcom(), "\n1 OBJE @" . $media->xref() . '@'); 146 147 case self::LINK_EVENT: 148 return str_contains($individual->gedcom(), "\n2 OBJE @" . $media->xref() . '@'); 149 } 150 } 151 152 return false; 153 }); 154 155 $random_media = null; 156 157 if ($random_row !== null) { 158 $random_media = Registry::mediaFactory()->make($random_row->m_id, $tree, $random_row->m_gedcom); 159 } 160 161 if ($random_media instanceof Media) { 162 $content = view('modules/random_media/slide-show', [ 163 'block_id' => $block_id, 164 'delay' => self::DELAY, 165 'linked_families' => $this->linked_record_service->linkedFamilies($random_media), 166 'linked_individuals' => $this->linked_record_service->linkedIndividuals($random_media), 167 'linked_sources' => $this->linked_record_service->linkedSources($random_media), 168 'media' => $random_media, 169 'media_file' => $random_media->firstImageFile(), 170 'show_controls' => $controls, 171 'start_automatically' => $start, 172 'tree' => $tree, 173 ]); 174 } else { 175 $content = I18N::translate('This family tree has no images to display.'); 176 } 177 178 if ($context !== self::CONTEXT_EMBED) { 179 return view('modules/block-template', [ 180 'block' => Str::kebab($this->name()), 181 'id' => $block_id, 182 'config_url' => $this->configUrl($tree, $context, $block_id), 183 'title' => $this->title(), 184 'content' => $content, 185 ]); 186 } 187 188 return $content; 189 } 190 191 /** 192 * How should this module be identified in the control panel, etc.? 193 * 194 * @return string 195 */ 196 public function title(): string 197 { 198 /* I18N: Name of a module */ 199 return I18N::translate('Slide show'); 200 } 201 202 /** 203 * Should this block load asynchronously using AJAX? 204 * 205 * Simple blocks are faster in-line, more complex ones can be loaded later. 206 * 207 * @return bool 208 */ 209 public function loadAjax(): bool 210 { 211 return true; 212 } 213 214 /** 215 * Can this block be shown on the user’s home page? 216 * 217 * @return bool 218 */ 219 public function isUserBlock(): bool 220 { 221 return true; 222 } 223 224 /** 225 * Can this block be shown on the tree’s home page? 226 * 227 * @return bool 228 */ 229 public function isTreeBlock(): bool 230 { 231 return true; 232 } 233 234 /** 235 * Update the configuration for a block. 236 * 237 * @param ServerRequestInterface $request 238 * @param int $block_id 239 * 240 * @return void 241 */ 242 public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 243 { 244 $this->setBlockSetting($block_id, 'filter', Validator::parsedBody($request)->string('filter')); 245 $this->setBlockSetting($block_id, 'controls', Validator::parsedBody($request)->string('controls')); 246 $this->setBlockSetting($block_id, 'start', Validator::parsedBody($request)->string('start')); 247 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_AUDIO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_AUDIO, false)); 248 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_BOOK), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_BOOK, false)); 249 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_CARD), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_CARD, false)); 250 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_CERTIFICATE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_CERTIFICATE, false)); 251 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_COAT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_COAT, false)); 252 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_DOCUMENT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_DOCUMENT, false)); 253 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_ELECTRONIC), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_ELECTRONIC, false)); 254 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_FICHE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_FICHE, false)); 255 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_FILM), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_FILM, false)); 256 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_MAGAZINE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_MAGAZINE, false)); 257 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_MANUSCRIPT), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_MANUSCRIPT, false)); 258 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_MAP), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_MAP, false)); 259 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_NEWSPAPER), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_NEWSPAPER, false)); 260 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_OTHER), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_OTHER, false)); 261 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_PAINTING), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_PAINTING, false)); 262 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_PHOTO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_PHOTO, false)); 263 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_TOMBSTONE), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_TOMBSTONE, false)); 264 $this->setBlockSetting($block_id, 'filter_' . strtolower(SourceMediaType::VALUE_VIDEO), (string) Validator::parsedBody($request)->boolean(SourceMediaType::VALUE_VIDEO, false)); 265 } 266 267 /** 268 * An HTML form to edit block settings 269 * 270 * @param Tree $tree 271 * @param int $block_id 272 * 273 * @return string 274 */ 275 public function editBlockConfiguration(Tree $tree, int $block_id): string 276 { 277 $filter = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); 278 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 279 $start = $this->getBlockSetting($block_id, 'start', '0'); 280 281 $filters = [ 282 SourceMediaType::VALUE_AUDIO => $this->getBlockSetting($block_id, 'filter_audio', '0'), 283 SourceMediaType::VALUE_BOOK => $this->getBlockSetting($block_id, 'filter_book', '1'), 284 SourceMediaType::VALUE_CARD => $this->getBlockSetting($block_id, 'filter_card', '1'), 285 SourceMediaType::VALUE_CERTIFICATE => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 286 SourceMediaType::VALUE_COAT => $this->getBlockSetting($block_id, 'filter_coat', '1'), 287 SourceMediaType::VALUE_DOCUMENT => $this->getBlockSetting($block_id, 'filter_document', '1'), 288 SourceMediaType::VALUE_ELECTRONIC => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 289 SourceMediaType::VALUE_FICHE => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 290 SourceMediaType::VALUE_FILM => $this->getBlockSetting($block_id, 'filter_film', '1'), 291 SourceMediaType::VALUE_MAGAZINE => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 292 SourceMediaType::VALUE_MANUSCRIPT => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 293 SourceMediaType::VALUE_MAP => $this->getBlockSetting($block_id, 'filter_map', '1'), 294 SourceMediaType::VALUE_NEWSPAPER => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 295 SourceMediaType::VALUE_OTHER => $this->getBlockSetting($block_id, 'filter_other', '1'), 296 SourceMediaType::VALUE_PAINTING => $this->getBlockSetting($block_id, 'filter_painting', '1'), 297 SourceMediaType::VALUE_PHOTO => $this->getBlockSetting($block_id, 'filter_photo', '1'), 298 SourceMediaType::VALUE_TOMBSTONE => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 299 SourceMediaType::VALUE_VIDEO => $this->getBlockSetting($block_id, 'filter_video', '0'), 300 ]; 301 302 $formats = array_filter(Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values()); 303 304 return view('modules/random_media/config', [ 305 'controls' => $controls, 306 'filter' => $filter, 307 'filters' => $filters, 308 'formats' => $formats, 309 'start' => $start, 310 ]); 311 } 312} 313