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