18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Media; 24495cefeaSGreg Roachuse Fisharebest\Webtrees\MediaFile; 25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree; 2631e43e2fSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2731e43e2fSGreg Roachuse Illuminate\Database\Query\JoinClause; 28a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class SlideShowModule 328c2e8227SGreg Roach */ 3337eb8894SGreg Roachclass SlideShowModule extends AbstractModule implements ModuleBlockInterface 34c1010edaSGreg Roach{ 3549a243cbSGreg Roach use ModuleBlockTrait; 3649a243cbSGreg Roach 37961ec755SGreg Roach /** 380cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 39961ec755SGreg Roach * 40961ec755SGreg Roach * @return string 41961ec755SGreg Roach */ 4249a243cbSGreg Roach public function title(): string 43c1010edaSGreg Roach { 44bbb76c12SGreg Roach /* I18N: Name of a module */ 45bbb76c12SGreg Roach return I18N::translate('Slide show'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 48961ec755SGreg Roach /** 49961ec755SGreg Roach * A sentence describing what this module does. 50961ec755SGreg Roach * 51961ec755SGreg Roach * @return string 52961ec755SGreg Roach */ 5349a243cbSGreg Roach public function description(): string 54c1010edaSGreg Roach { 55bbb76c12SGreg Roach /* I18N: Description of the “Slide show” module */ 56bbb76c12SGreg Roach return I18N::translate('Random images from the current family tree.'); 578c2e8227SGreg Roach } 588c2e8227SGreg Roach 5976692c8bSGreg Roach /** 6076692c8bSGreg Roach * Generate the HTML content of this block. 6176692c8bSGreg Roach * 62e490cd80SGreg Roach * @param Tree $tree 6376692c8bSGreg Roach * @param int $block_id 645f2ae573SGreg Roach * @param string $ctype 65727f238cSGreg Roach * @param string[] $cfg 6676692c8bSGreg Roach * 6776692c8bSGreg Roach * @return string 6876692c8bSGreg Roach */ 695f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 70c1010edaSGreg Roach { 71e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 72e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 736ebd34c5SGreg Roach $start = (bool) $this->getBlockSetting($block_id, 'start', '0'); 748c2e8227SGreg Roach 7531e43e2fSGreg Roach $media_types = [ 76e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 77e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 78e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 79e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 80e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 81e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 82e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 83e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 84e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 85e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 86e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 87e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 88e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 89e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 90e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 91e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 92e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 93e2a378d3SGreg Roach $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 9431e43e2fSGreg Roach ]; 9531e43e2fSGreg Roach 9631e43e2fSGreg Roach $media_types = array_filter($media_types); 9731e43e2fSGreg Roach 9831e43e2fSGreg Roach // We can apply the filters using SQL 9931e43e2fSGreg Roach // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 10031e43e2fSGreg Roach $all_media = DB::table('media') 10131e43e2fSGreg Roach ->join('media_file', function (JoinClause $join): void { 10231e43e2fSGreg Roach $join 10331e43e2fSGreg Roach ->on('media_file.m_file', '=', 'media.m_file') 10431e43e2fSGreg Roach ->on('media_file.m_id', '=', 'media.m_id'); 10531e43e2fSGreg Roach }) 10631e43e2fSGreg Roach ->where('media.m_file', '=', $tree->id()) 10731e43e2fSGreg Roach ->whereIn('media_file.multimedia_format', ['jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp']) 10831e43e2fSGreg Roach ->whereIn('media_file.source_media_type', $media_types) 10931e43e2fSGreg Roach ->pluck('media.m_id') 11031e43e2fSGreg Roach ->all(); 1118c2e8227SGreg Roach 1128c2e8227SGreg Roach // Keep looking through the media until a suitable one is found. 1138c2e8227SGreg Roach $random_media = null; 114495cefeaSGreg Roach while (!empty($all_media)) { 1158c2e8227SGreg Roach $n = array_rand($all_media); 116e490cd80SGreg Roach $media = Media::getInstance($all_media[$n], $tree); 117572a608bSGreg Roach $media_file = $media->firstImageFile(); 118495cefeaSGreg Roach if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) { 1198c2e8227SGreg Roach // Check if it is linked to a suitable individual 1208c2e8227SGreg Roach foreach ($media->linkedIndividuals('OBJE') as $indi) { 1218c2e8227SGreg Roach if ( 1228c2e8227SGreg Roach $filter === 'all' || 1237d0db648SGreg Roach $filter === 'indi' && strpos($indi->gedcom(), "\n1 OBJE @" . $media->xref() . '@') !== false || 1247d0db648SGreg Roach $filter === 'event' && strpos($indi->gedcom(), "\n2 OBJE @" . $media->xref() . '@') !== false 1258c2e8227SGreg Roach ) { 1268c2e8227SGreg Roach // Found one :-) 1278c2e8227SGreg Roach $random_media = $media; 1288c2e8227SGreg Roach break 2; 1298c2e8227SGreg Roach } 1308c2e8227SGreg Roach } 1318c2e8227SGreg Roach } 1328c2e8227SGreg Roach unset($all_media[$n]); 1333c12f3e5SGreg Roach } 1348c2e8227SGreg Roach 1358c2e8227SGreg Roach if ($random_media) { 136147e99aaSGreg Roach $content = view('modules/random_media/slide-show', [ 137a1fe7073SGreg Roach 'block_id' => $block_id, 138a1fe7073SGreg Roach 'media' => $random_media, 139495cefeaSGreg Roach 'media_file' => $random_media->firstImageFile(), 140a1fe7073SGreg Roach 'show_controls' => $controls, 141a1fe7073SGreg Roach 'start_automatically' => $start, 142911f5683SGreg Roach 'tree' => $tree, 143a1fe7073SGreg Roach ]); 1448c2e8227SGreg Roach } else { 1458c2e8227SGreg Roach $content = I18N::translate('This family tree has no images to display.'); 1468c2e8227SGreg Roach } 147a1fe7073SGreg Roach 1486a8879feSGreg Roach if ($ctype !== '') { 149e490cd80SGreg Roach if ($ctype === 'gedcom' && Auth::isManager($tree)) { 150c1010edaSGreg Roach $config_url = route('tree-page-block-edit', [ 151c1010edaSGreg Roach 'block_id' => $block_id, 152aa6f03bbSGreg Roach 'ged' => $tree->name(), 153c1010edaSGreg Roach ]); 154397e599aSGreg Roach } elseif ($ctype === 'user' && Auth::check()) { 155c1010edaSGreg Roach $config_url = route('user-page-block-edit', [ 156c1010edaSGreg Roach 'block_id' => $block_id, 157aa6f03bbSGreg Roach 'ged' => $tree->name(), 158c1010edaSGreg Roach ]); 1598cbbfdceSGreg Roach } else { 1608cbbfdceSGreg Roach $config_url = ''; 1618cbbfdceSGreg Roach } 1628cbbfdceSGreg Roach 163147e99aaSGreg Roach return view('modules/block-template', [ 16426684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 1659c6524dcSGreg Roach 'id' => $block_id, 1668cbbfdceSGreg Roach 'config_url' => $config_url, 16749a243cbSGreg Roach 'title' => $this->title(), 1689c6524dcSGreg Roach 'content' => $content, 1699c6524dcSGreg Roach ]); 1708c2e8227SGreg Roach } 171b2ce94c6SRico Sonntag 172b2ce94c6SRico Sonntag return $content; 1738c2e8227SGreg Roach } 1748c2e8227SGreg Roach 1758c2e8227SGreg Roach /** {@inheritdoc} */ 176c1010edaSGreg Roach public function loadAjax(): bool 177c1010edaSGreg Roach { 1788c2e8227SGreg Roach return true; 1798c2e8227SGreg Roach } 1808c2e8227SGreg Roach 1818c2e8227SGreg Roach /** {@inheritdoc} */ 182c1010edaSGreg Roach public function isUserBlock(): bool 183c1010edaSGreg Roach { 1848c2e8227SGreg Roach return true; 1858c2e8227SGreg Roach } 1868c2e8227SGreg Roach 1878c2e8227SGreg Roach /** {@inheritdoc} */ 18863276d8fSGreg Roach public function isTreeBlock(): bool 189c1010edaSGreg Roach { 1908c2e8227SGreg Roach return true; 1918c2e8227SGreg Roach } 1928c2e8227SGreg Roach 19376692c8bSGreg Roach /** 194a45f9889SGreg Roach * Update the configuration for a block. 195a45f9889SGreg Roach * 196a45f9889SGreg Roach * @param Request $request 197a45f9889SGreg Roach * @param int $block_id 198a45f9889SGreg Roach * 199a45f9889SGreg Roach * @return void 200a45f9889SGreg Roach */ 201*e364afe4SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id): void 202a45f9889SGreg Roach { 203a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all')); 204a45f9889SGreg Roach $this->setBlockSetting($block_id, 'controls', $request->get('controls', '')); 205a45f9889SGreg Roach $this->setBlockSetting($block_id, 'start', $request->get('start', '')); 206a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', '')); 207a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', '')); 208a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', '')); 209a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', '')); 210a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', '')); 211a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', '')); 212a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', '')); 213a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', '')); 214a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', '')); 215a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', '')); 216a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', '')); 217a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', '')); 218a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', '')); 219a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', '')); 220a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', '')); 221a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', '')); 222a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', '')); 223a45f9889SGreg Roach $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', '')); 224a45f9889SGreg Roach } 225a45f9889SGreg Roach 226a45f9889SGreg Roach /** 22776692c8bSGreg Roach * An HTML form to edit block settings 22876692c8bSGreg Roach * 229e490cd80SGreg Roach * @param Tree $tree 23076692c8bSGreg Roach * @param int $block_id 231a9430be8SGreg Roach * 232a9430be8SGreg Roach * @return void 23376692c8bSGreg Roach */ 234*e364afe4SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): void 235c1010edaSGreg Roach { 236e2a378d3SGreg Roach $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 237e2a378d3SGreg Roach $controls = $this->getBlockSetting($block_id, 'controls', '1'); 23815d603e7SGreg Roach $start = $this->getBlockSetting($block_id, 'start', '0'); 2398c2e8227SGreg Roach 24013abd6f3SGreg Roach $filters = [ 241e2a378d3SGreg Roach 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 242e2a378d3SGreg Roach 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 243e2a378d3SGreg Roach 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 244e2a378d3SGreg Roach 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 245e2a378d3SGreg Roach 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 246e2a378d3SGreg Roach 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 247e2a378d3SGreg Roach 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 248e2a378d3SGreg Roach 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 249e2a378d3SGreg Roach 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 250e2a378d3SGreg Roach 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 251e2a378d3SGreg Roach 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 252e2a378d3SGreg Roach 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 253e2a378d3SGreg Roach 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 254e2a378d3SGreg Roach 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 255e2a378d3SGreg Roach 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 256e2a378d3SGreg Roach 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 257e2a378d3SGreg Roach 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 258e2a378d3SGreg Roach 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 25913abd6f3SGreg Roach ]; 2608c2e8227SGreg Roach 261c385536dSGreg Roach $formats = GedcomTag::getFileFormTypes(); 26215d603e7SGreg Roach 263147e99aaSGreg Roach echo view('modules/random_media/config', [ 264c385536dSGreg Roach 'controls' => $controls, 265c385536dSGreg Roach 'filter' => $filter, 266c385536dSGreg Roach 'filters' => $filters, 267c385536dSGreg Roach 'formats' => $formats, 268c385536dSGreg Roach 'start' => $start, 269c385536dSGreg Roach ]); 2708c2e8227SGreg Roach } 2718c2e8227SGreg Roach} 272