1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Database; 22use Fisharebest\Webtrees\GedcomTag; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Media; 25use Fisharebest\Webtrees\MediaFile; 26use Fisharebest\Webtrees\Tree; 27use Symfony\Component\HttpFoundation\Request; 28 29/** 30 * Class SlideShowModule 31 */ 32class SlideShowModule extends AbstractModule implements ModuleBlockInterface 33{ 34 /** {@inheritdoc} */ 35 public function getTitle(): string 36 { 37 /* I18N: Name of a module */ 38 return I18N::translate('Slide show'); 39 } 40 41 /** {@inheritdoc} */ 42 public function getDescription(): string 43 { 44 /* I18N: Description of the “Slide show” module */ 45 return I18N::translate('Random images from the current family tree.'); 46 } 47 48 /** 49 * Generate the HTML content of this block. 50 * 51 * @param Tree $tree 52 * @param int $block_id 53 * @param bool $template 54 * @param string[] $cfg 55 * 56 * @return string 57 */ 58 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 59 { 60 global $ctype; 61 62 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 63 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 64 $start = (bool) $this->getBlockSetting($block_id, 'start', '0'); 65 66 // We can apply the filters using SQL 67 // Do not use "ORDER BY RAND()" - it is very slow on large tables. Use PHP::array_rand() instead. 68 $all_media = Database::prepare( 69 "SELECT m_id FROM `##media`" . 70 " JOIN `##media_file` USING (m_file, m_id)" . 71 " WHERE m_file = ?" . 72 " AND multimedia_format IN ('jpg', 'jpeg', 'png', 'gif', 'tiff', 'bmp')" . 73 " AND source_media_type IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '')" 74 )->execute([ 75 $tree->getTreeId(), 76 $this->getBlockSetting($block_id, 'filter_audio', '0') ? 'audio' : null, 77 $this->getBlockSetting($block_id, 'filter_book', '1') ? 'book' : null, 78 $this->getBlockSetting($block_id, 'filter_card', '1') ? 'card' : null, 79 $this->getBlockSetting($block_id, 'filter_certificate', '1') ? 'certificate' : null, 80 $this->getBlockSetting($block_id, 'filter_coat', '1') ? 'coat' : null, 81 $this->getBlockSetting($block_id, 'filter_document', '1') ? 'document' : null, 82 $this->getBlockSetting($block_id, 'filter_electronic', '1') ? 'electronic' : null, 83 $this->getBlockSetting($block_id, 'filter_fiche', '1') ? 'fiche' : null, 84 $this->getBlockSetting($block_id, 'filter_film', '1') ? 'film' : null, 85 $this->getBlockSetting($block_id, 'filter_magazine', '1') ? 'magazine' : null, 86 $this->getBlockSetting($block_id, 'filter_manuscript', '1') ? 'manuscript' : null, 87 $this->getBlockSetting($block_id, 'filter_map', '1') ? 'map' : null, 88 $this->getBlockSetting($block_id, 'filter_newspaper', '1') ? 'newspaper' : null, 89 $this->getBlockSetting($block_id, 'filter_other', '1') ? 'other' : null, 90 $this->getBlockSetting($block_id, 'filter_painting', '1') ? 'painting' : null, 91 $this->getBlockSetting($block_id, 'filter_photo', '1') ? 'photo' : null, 92 $this->getBlockSetting($block_id, 'filter_tombstone', '1') ? 'tombstone' : null, 93 $this->getBlockSetting($block_id, 'filter_video', '0') ? 'video' : null, 94 ])->fetchOneColumn(); 95 96 // Keep looking through the media until a suitable one is found. 97 $random_media = null; 98 while (!empty($all_media)) { 99 $n = array_rand($all_media); 100 $media = Media::getInstance($all_media[$n], $tree); 101 $media_file = $media->firstImageFile(); 102 if ($media->canShow() && $media_file instanceof MediaFile && !$media_file->isExternal()) { 103 // Check if it is linked to a suitable individual 104 foreach ($media->linkedIndividuals('OBJE') as $indi) { 105 if ( 106 $filter === 'all' || 107 $filter === 'indi' && strpos($indi->getGedcom(), "\n1 OBJE @" . $media->getXref() . '@') !== false || 108 $filter === 'event' && strpos($indi->getGedcom(), "\n2 OBJE @" . $media->getXref() . '@') !== false 109 ) { 110 // Found one :-) 111 $random_media = $media; 112 break 2; 113 } 114 } 115 } 116 unset($all_media[$n]); 117 } 118 119 if ($random_media) { 120 $content = view('modules/random_media/slide-show', [ 121 'block_id' => $block_id, 122 'media' => $random_media, 123 'media_file' => $random_media->firstImageFile(), 124 'show_controls' => $controls, 125 'start_automatically' => $start, 126 'tree' => $tree, 127 ]); 128 } else { 129 $content = I18N::translate('This family tree has no images to display.'); 130 } 131 132 if ($template) { 133 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 134 $config_url = route('tree-page-block-edit', [ 135 'block_id' => $block_id, 136 'ged' => $tree->getName(), 137 ]); 138 } elseif ($ctype === 'user' && Auth::check()) { 139 $config_url = route('user-page-block-edit', [ 140 'block_id' => $block_id, 141 'ged' => $tree->getName(), 142 ]); 143 } else { 144 $config_url = ''; 145 } 146 147 return view('modules/block-template', [ 148 'block' => str_replace('_', '-', $this->getName()), 149 'id' => $block_id, 150 'config_url' => $config_url, 151 'title' => $this->getTitle(), 152 'content' => $content, 153 ]); 154 } 155 156 return $content; 157 } 158 159 /** {@inheritdoc} */ 160 public function loadAjax(): bool 161 { 162 return true; 163 } 164 165 /** {@inheritdoc} */ 166 public function isUserBlock(): bool 167 { 168 return true; 169 } 170 171 /** {@inheritdoc} */ 172 public function isGedcomBlock(): bool 173 { 174 return true; 175 } 176 177 /** 178 * Update the configuration for a block. 179 * 180 * @param Request $request 181 * @param int $block_id 182 * 183 * @return void 184 */ 185 public function saveBlockConfiguration(Request $request, int $block_id) 186 { 187 $this->setBlockSetting($block_id, 'filter', $request->get('filter', 'all')); 188 $this->setBlockSetting($block_id, 'controls', $request->get('controls', '')); 189 $this->setBlockSetting($block_id, 'start', $request->get('start', '')); 190 $this->setBlockSetting($block_id, 'filter_audio', $request->get('filter_audio', '')); 191 $this->setBlockSetting($block_id, 'filter_book', $request->get('filter_book', '')); 192 $this->setBlockSetting($block_id, 'filter_card', $request->get('filter_card', '')); 193 $this->setBlockSetting($block_id, 'filter_certificate', $request->get('filter_certificate', '')); 194 $this->setBlockSetting($block_id, 'filter_coat', $request->get('filter_coat', '')); 195 $this->setBlockSetting($block_id, 'filter_document', $request->get('filter_document', '')); 196 $this->setBlockSetting($block_id, 'filter_electronic', $request->get('filter_electronic', '')); 197 $this->setBlockSetting($block_id, 'filter_fiche', $request->get('filter_fiche', '')); 198 $this->setBlockSetting($block_id, 'filter_film', $request->get('filter_film', '')); 199 $this->setBlockSetting($block_id, 'filter_magazine', $request->get('filter_magazine', '')); 200 $this->setBlockSetting($block_id, 'filter_manuscript', $request->get('filter_manuscript', '')); 201 $this->setBlockSetting($block_id, 'filter_map', $request->get('filter_map', '')); 202 $this->setBlockSetting($block_id, 'filter_newspaper', $request->get('filter_newspaper', '')); 203 $this->setBlockSetting($block_id, 'filter_other', $request->get('filter_other', '')); 204 $this->setBlockSetting($block_id, 'filter_painting', $request->get('filter_painting', '')); 205 $this->setBlockSetting($block_id, 'filter_photo', $request->get('filter_photo', '')); 206 $this->setBlockSetting($block_id, 'filter_tombstone', $request->get('filter_tombstone', '')); 207 $this->setBlockSetting($block_id, 'filter_video', $request->get('filter_video', '')); 208 } 209 210 /** 211 * An HTML form to edit block settings 212 * 213 * @param Tree $tree 214 * @param int $block_id 215 * 216 * @return void 217 */ 218 public function editBlockConfiguration(Tree $tree, int $block_id) 219 { 220 $filter = $this->getBlockSetting($block_id, 'filter', 'all'); 221 $controls = $this->getBlockSetting($block_id, 'controls', '1'); 222 $start = $this->getBlockSetting($block_id, 'start', '0'); 223 224 $filters = [ 225 'audio' => $this->getBlockSetting($block_id, 'filter_audio', '0'), 226 'book' => $this->getBlockSetting($block_id, 'filter_book', '1'), 227 'card' => $this->getBlockSetting($block_id, 'filter_card', '1'), 228 'certificate' => $this->getBlockSetting($block_id, 'filter_certificate', '1'), 229 'coat' => $this->getBlockSetting($block_id, 'filter_coat', '1'), 230 'document' => $this->getBlockSetting($block_id, 'filter_document', '1'), 231 'electronic' => $this->getBlockSetting($block_id, 'filter_electronic', '1'), 232 'fiche' => $this->getBlockSetting($block_id, 'filter_fiche', '1'), 233 'film' => $this->getBlockSetting($block_id, 'filter_film', '1'), 234 'magazine' => $this->getBlockSetting($block_id, 'filter_magazine', '1'), 235 'manuscript' => $this->getBlockSetting($block_id, 'filter_manuscript', '1'), 236 'map' => $this->getBlockSetting($block_id, 'filter_map', '1'), 237 'newspaper' => $this->getBlockSetting($block_id, 'filter_newspaper', '1'), 238 'other' => $this->getBlockSetting($block_id, 'filter_other', '1'), 239 'painting' => $this->getBlockSetting($block_id, 'filter_painting', '1'), 240 'photo' => $this->getBlockSetting($block_id, 'filter_photo', '1'), 241 'tombstone' => $this->getBlockSetting($block_id, 'filter_tombstone', '1'), 242 'video' => $this->getBlockSetting($block_id, 'filter_video', '0'), 243 ]; 244 245 $formats = GedcomTag::getFileFormTypes(); 246 247 echo view('modules/random_media/config', [ 248 'controls' => $controls, 249 'filter' => $filter, 250 'filters' => $filters, 251 'formats' => $formats, 252 'start' => $start, 253 ]); 254 } 255} 256