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