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