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