18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 2409482a55SGreg Roachuse Fisharebest\Webtrees\Registry; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 2631e43e2fSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2731e43e2fSGreg Roachuse Illuminate\Database\Query\JoinClause; 281e7a7a28SGreg Roachuse Illuminate\Support\Str; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 303976b470SGreg Roach 316ccdf4f0SGreg Roachuse function app; 32455a30feSGreg Roachuse function array_filter; 337ff867dcSGreg Roachuse function assert; 349e8577e7SGreg Roachuse function in_array; 35dec352c1SGreg Roachuse function str_contains; 368c2e8227SGreg Roach 378c2e8227SGreg Roach/** 388c2e8227SGreg Roach * Class SlideShowModule 398c2e8227SGreg Roach */ 4037eb8894SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface 41c1010edaSGreg Roach{ 4249a243cbSGreg Roach use ModuleBlockTrait; 4349a243cbSGreg Roach 447ff867dcSGreg Roach // Show media linked to events or individuals. 457ff867dcSGreg Roach private const LINK_ALL = 'all'; 467ff867dcSGreg Roach private const LINK_EVENT = 'event'; 477ff867dcSGreg Roach private const LINK_INDIVIDUAL = 'indi'; 487ff867dcSGreg Roach 497ff867dcSGreg Roach // How long to show each slide (seconds) 507ff867dcSGreg Roach private const DELAY = 6; 517ff867dcSGreg Roach 52961ec755SGreg Roach /** 53961ec755SGreg Roach * A sentence describing what this module does. 54961ec755SGreg Roach * 55961ec755SGreg Roach * @return string 56961ec755SGreg Roach */ 5749a243cbSGreg Roach public function description(): string 58c1010edaSGreg Roach { 59bbb76c12SGreg Roach /* I18N: Description of the “Slide show” module */ 60bbb76c12SGreg Roach return I18N::translate('Random images from the current family tree.'); 618c2e8227SGreg Roach } 628c2e8227SGreg Roach 6376692c8bSGreg Roach /** 6476692c8bSGreg Roach * Generate the HTML content of this block. 6576692c8bSGreg Roach * 66e490cd80SGreg Roach * @param Tree $tree 6776692c8bSGreg Roach * @param int $block_id 683caaa4d2SGreg Roach * @param string $context 69*76d39c55SGreg Roach * @param array<string,string> $config 7076692c8bSGreg Roach * 7176692c8bSGreg Roach * @return string 7276692c8bSGreg Roach */ 733caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 74c1010edaSGreg Roach { 756ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 76b6b9dcc9SGreg Roach $default_start = $this->getBlockSetting($block_id, 'start'); 777ff867dcSGreg Roach $filter_links = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); 78e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 79b6b9dcc9SGreg Roach $start = (bool) ($request->getQueryParams()['start'] ?? $default_start); 808c2e8227SGreg Roach 817ff867dcSGreg Roach $filter_types = [ 82e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 83e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 84e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 85e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 86e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 87e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 88e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 89e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 90e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 91e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 92e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 93e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 94e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 95e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 96e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 97e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 98e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 99e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 10031e43e2fSGreg Roach ]; 10131e43e2fSGreg Roach 1027ff867dcSGreg Roach $filter_types = array_filter($filter_types); 10331e43e2fSGreg Roach 1049e8577e7SGreg Roach // The type "other" includes media without a type. 1057ff867dcSGreg Roach if (in_array('other', $filter_types, true)) { 1067ff867dcSGreg Roach $filter_types[] = ''; 1079e8577e7SGreg Roach } 1089e8577e7SGreg Roach 1095136fb17SGreg Roach // We can apply the filters using SQL, but it is more efficient to shuffle in PHP. 1105136fb17SGreg Roach $random_row = DB::table('media') 1110b5fd0a6SGreg Roach ->join('media_file', static function (JoinClause $join): void { 11231e43e2fSGreg Roach $join 11331e43e2fSGreg Roach ->on('media_file.m_file', '=', 'media.m_file') 11431e43e2fSGreg Roach ->on('media_file.m_id', '=', 'media.m_id'); 11531e43e2fSGreg Roach }) 11631e43e2fSGreg Roach ->where('media.m_file', '=', $tree->id()) 11731e43e2fSGreg Roach ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp']) 1187ff867dcSGreg Roach ->whereIn('media_file.source_media_type', $filter_types) 1195136fb17SGreg Roach ->select('media.*') 1205136fb17SGreg Roach ->get() 1215136fb17SGreg Roach ->shuffle() 122f70bcff5SGreg Roach ->first(static function (object $row) use ($filter_links, $tree): bool { 1236b9cb339SGreg Roach $media = Registry::mediaFactory()->make($row->m_id, $tree, $row->m_gedcom); 1247ff867dcSGreg Roach assert($media instanceof Media); 1257ff867dcSGreg Roach 126615ea171SGreg Roach if (!$media->canShow() || $media->firstImageFile() === null) { 1277ff867dcSGreg Roach return false; 1287ff867dcSGreg Roach } 1298c2e8227SGreg Roach 1305136fb17SGreg Roach foreach ($media->linkedIndividuals('OBJE') as $individual) { 1317ff867dcSGreg Roach switch ($filter_links) { 1327ff867dcSGreg Roach case self::LINK_ALL: 1335136fb17SGreg Roach return true; 1347ff867dcSGreg Roach 1357ff867dcSGreg Roach case self::LINK_INDIVIDUAL: 136dec352c1SGreg Roach return str_contains($individual->gedcom(), "\n1 OBJE @" . $media->xref() . '@'); 1377ff867dcSGreg Roach 1387ff867dcSGreg Roach case self::LINK_EVENT: 139dec352c1SGreg Roach return str_contains($individual->gedcom(), "\n2 OBJE @" . $media->xref() . '@'); 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach 1435136fb17SGreg Roach return false; 1445136fb17SGreg Roach }); 1455136fb17SGreg Roach 146ae7e1e94SGreg Roach $random_media = null; 147ae7e1e94SGreg Roach 148ae7e1e94SGreg Roach if ($random_row !== null) { 1496b9cb339SGreg Roach $random_media = Registry::mediaFactory()->make($random_row->m_id, $tree, $random_row->m_gedcom); 150ae7e1e94SGreg Roach } 1515136fb17SGreg Roach 1525136fb17SGreg Roach if ($random_media instanceof Media) { 153147e99aaSGreg Roach $content = view('modules/random_media/slide-show', [ 154a1fe7073SGreg Roach 'block_id' => $block_id, 1557ff867dcSGreg Roach 'delay' => self::DELAY, 156a1fe7073SGreg Roach 'media' => $random_media, 157495cefeaSGreg Roach 'media_file' => $random_media->firstImageFile(), 158a1fe7073SGreg Roach 'show_controls' => $controls, 159a1fe7073SGreg Roach 'start_automatically' => $start, 160911f5683SGreg Roach 'tree' => $tree, 161a1fe7073SGreg Roach ]); 1628c2e8227SGreg Roach } else { 1638c2e8227SGreg Roach $content = I18N::translate('This family tree has no images to display.'); 1648c2e8227SGreg Roach } 165a1fe7073SGreg Roach 1663caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 167147e99aaSGreg Roach return view('modules/block-template', [ 1681e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 1699c6524dcSGreg Roach 'id' => $block_id, 1703caaa4d2SGreg Roach 'config_url' => $this->configUrl($tree, $context, $block_id), 17149a243cbSGreg Roach 'title' => $this->title(), 1729c6524dcSGreg Roach 'content' => $content, 1739c6524dcSGreg Roach ]); 1748c2e8227SGreg Roach } 175b2ce94c6SRico Sonntag 176b2ce94c6SRico Sonntag return $content; 1778c2e8227SGreg Roach } 1788c2e8227SGreg Roach 1796ccdf4f0SGreg Roach /** 1806ccdf4f0SGreg Roach * How should this module be identified in the control panel, etc.? 1816ccdf4f0SGreg Roach * 1826ccdf4f0SGreg Roach * @return string 1836ccdf4f0SGreg Roach */ 1846ccdf4f0SGreg Roach public function title(): string 1856ccdf4f0SGreg Roach { 1866ccdf4f0SGreg Roach /* I18N: Name of a module */ 1876ccdf4f0SGreg Roach return I18N::translate('Slide show'); 1886ccdf4f0SGreg Roach } 1896ccdf4f0SGreg Roach 1903caaa4d2SGreg Roach /** 1913caaa4d2SGreg Roach * Should this block load asynchronously using AJAX? 1923caaa4d2SGreg Roach * 1933caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 1943caaa4d2SGreg Roach * 1953caaa4d2SGreg Roach * @return bool 1963caaa4d2SGreg Roach */ 197c1010edaSGreg Roach public function loadAjax(): bool 198c1010edaSGreg Roach { 1998c2e8227SGreg Roach return true; 2008c2e8227SGreg Roach } 2018c2e8227SGreg Roach 2023caaa4d2SGreg Roach /** 2033caaa4d2SGreg Roach * Can this block be shown on the user’s home page? 2043caaa4d2SGreg Roach * 2053caaa4d2SGreg Roach * @return bool 2063caaa4d2SGreg Roach */ 207c1010edaSGreg Roach public function isUserBlock(): bool 208c1010edaSGreg Roach { 2098c2e8227SGreg Roach return true; 2108c2e8227SGreg Roach } 2118c2e8227SGreg Roach 2123caaa4d2SGreg Roach /** 2133caaa4d2SGreg Roach * Can this block be shown on the tree’s home page? 2143caaa4d2SGreg Roach * 2153caaa4d2SGreg Roach * @return bool 2163caaa4d2SGreg Roach */ 21763276d8fSGreg Roach public function isTreeBlock(): bool 218c1010edaSGreg Roach { 2198c2e8227SGreg Roach return true; 2208c2e8227SGreg Roach } 2218c2e8227SGreg Roach 22276692c8bSGreg Roach /** 223a45f9889SGreg Roach * Update the configuration for a block. 224a45f9889SGreg Roach * 2256ccdf4f0SGreg Roach * @param ServerRequestInterface $request 226a45f9889SGreg Roach * @param int $block_id 227a45f9889SGreg Roach * 228a45f9889SGreg Roach * @return void 229a45f9889SGreg Roach */ 2306ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 231a45f9889SGreg Roach { 232b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 233b6b9dcc9SGreg Roach 234b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter', $params['filter']); 235b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'controls', $params['controls']); 236b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'start', $params['start']); 237b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_audio', $params['filter_audio'] ?? ''); 238b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_book', $params['filter_book'] ?? ''); 239b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_card', $params['filter_card'] ?? ''); 240b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_certificate', $params['filter_certificate'] ?? ''); 241b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_coat', $params['filter_coat'] ?? ''); 242b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_document', $params['filter_document'] ?? ''); 243b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_electronic', $params['filter_electronic'] ?? ''); 244b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_fiche', $params['filter_fiche'] ?? ''); 245b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_film', $params['filter_film'] ?? ''); 246b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_magazine', $params['filter_magazine'] ?? ''); 247b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_manuscript', $params['filter_manuscript'] ?? ''); 248b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_map', $params['filter_map'] ?? ''); 249b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_newspaper', $params['filter_newspaper'] ?? ''); 250b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_other', $params['filter_other'] ?? ''); 251b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_painting', $params['filter_painting'] ?? ''); 252b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_photo', $params['filter_photo'] ?? ''); 253b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_tombstone', $params['filter_tombstone'] ?? ''); 254b6b9dcc9SGreg Roach $this->setBlockSetting($block_id, 'filter_video', $params['filter_video'] ?? ''); 255a45f9889SGreg Roach } 256a45f9889SGreg Roach 257a45f9889SGreg Roach /** 25876692c8bSGreg Roach * An HTML form to edit block settings 25976692c8bSGreg Roach * 260e490cd80SGreg Roach * @param Tree $tree 26176692c8bSGreg Roach * @param int $block_id 262a9430be8SGreg Roach * 2633caaa4d2SGreg Roach * @return string 26476692c8bSGreg Roach */ 2653caaa4d2SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): string 266c1010edaSGreg Roach { 2677ff867dcSGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', self::LINK_ALL); 268e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 26915d603e7SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0'); 2708c2e8227SGreg Roach 27113abd6f3SGreg Roach $filters = [ 272e2a378d3SGreg Roach 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 273e2a378d3SGreg Roach 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 274e2a378d3SGreg Roach 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 275e2a378d3SGreg Roach 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 276e2a378d3SGreg Roach 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 277e2a378d3SGreg Roach 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 278e2a378d3SGreg Roach 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 279e2a378d3SGreg Roach 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 280e2a378d3SGreg Roach 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 281e2a378d3SGreg Roach 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 282e2a378d3SGreg Roach 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 283e2a378d3SGreg Roach 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 284e2a378d3SGreg Roach 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 285e2a378d3SGreg Roach 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 286e2a378d3SGreg Roach 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 287e2a378d3SGreg Roach 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 288e2a378d3SGreg Roach 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 289e2a378d3SGreg Roach 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 29013abd6f3SGreg Roach ]; 2918c2e8227SGreg Roach 292455a30feSGreg Roach $formats = array_filter(Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values()); 29315d603e7SGreg Roach 2943caaa4d2SGreg Roach return view('modules/random_media/config', [ 295c385536dSGreg Roach 'controls' => $controls, 296c385536dSGreg Roach 'filter' => $filter, 297c385536dSGreg Roach 'filters' => $filters, 298c385536dSGreg Roach 'formats' => $formats, 299c385536dSGreg Roach 'start' => $start, 300c385536dSGreg Roach ]); 3018c2e8227SGreg Roach } 3028c2e8227SGreg Roach} 303