1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fig\Http\Message\RequestMethodInterface; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\GedcomRecord; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Media; 27use Fisharebest\Webtrees\Registry; 28use Fisharebest\Webtrees\Services\LinkedRecordService; 29use Fisharebest\Webtrees\Tree; 30use Fisharebest\Webtrees\Validator; 31use Illuminate\Database\Capsule\Manager as DB; 32use Illuminate\Database\Query\Builder; 33use Illuminate\Database\Query\JoinClause; 34use Illuminate\Support\Collection; 35use Psr\Http\Message\ResponseInterface; 36use Psr\Http\Message\ServerRequestInterface; 37use Psr\Http\Server\RequestHandlerInterface; 38 39use function addcslashes; 40use function array_combine; 41use function array_unshift; 42use function dirname; 43use function max; 44use function min; 45use function redirect; 46use function route; 47 48/** 49 * Class MediaListModule 50 */ 51class MediaListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface 52{ 53 use ModuleListTrait; 54 55 protected const ROUTE_URL = '/tree/{tree}/media-list'; 56 57 private LinkedRecordService $linked_record_service; 58 59 /** 60 * @param LinkedRecordService $linked_record_service 61 */ 62 public function __construct(LinkedRecordService $linked_record_service) 63 { 64 $this->linked_record_service = $linked_record_service; 65 } 66 67 /** 68 * Initialization. 69 * 70 * @return void 71 */ 72 public function boot(): void 73 { 74 Registry::routeFactory()->routeMap() 75 ->get(static::class, static::ROUTE_URL, $this) 76 ->allows(RequestMethodInterface::METHOD_POST); 77 } 78 79 /** 80 * How should this module be identified in the control panel, etc.? 81 * 82 * @return string 83 */ 84 public function title(): string 85 { 86 /* I18N: Name of a module/list */ 87 return I18N::translate('Media objects'); 88 } 89 90 /** 91 * A sentence describing what this module does. 92 * 93 * @return string 94 */ 95 public function description(): string 96 { 97 /* I18N: Description of the “Media objects” module */ 98 return I18N::translate('A list of media objects.'); 99 } 100 101 /** 102 * CSS class for the URL. 103 * 104 * @return string 105 */ 106 public function listMenuClass(): string 107 { 108 return 'menu-list-obje'; 109 } 110 111 /** 112 * @param Tree $tree 113 * @param array<bool|int|string|array<string>|null> $parameters 114 * 115 * @return string 116 */ 117 public function listUrl(Tree $tree, array $parameters = []): string 118 { 119 $parameters['tree'] = $tree->name(); 120 121 return route(static::class, $parameters); 122 } 123 124 /** 125 * @return array<string> 126 */ 127 public function listUrlAttributes(): array 128 { 129 return []; 130 } 131 132 /** 133 * @param Tree $tree 134 * 135 * @return bool 136 */ 137 public function listIsEmpty(Tree $tree): bool 138 { 139 return !DB::table('media') 140 ->where('m_file', '=', $tree->id()) 141 ->exists(); 142 } 143 144 /** 145 * Handle URLs generated by older versions of webtrees 146 * 147 * @param ServerRequestInterface $request 148 * 149 * @return ResponseInterface 150 */ 151 public function getListAction(ServerRequestInterface $request): ResponseInterface 152 { 153 $tree = Validator::attributes($request)->tree(); 154 155 return redirect($this->listUrl($tree, $request->getQueryParams())); 156 } 157 158 /** 159 * @param ServerRequestInterface $request 160 * 161 * @return ResponseInterface 162 */ 163 public function handle(ServerRequestInterface $request): ResponseInterface 164 { 165 $tree = Validator::attributes($request)->tree(); 166 $user = Validator::attributes($request)->user(); 167 168 $data_filesystem = Registry::filesystem()->data(); 169 170 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 171 172 // Convert POST requests into GET requests for pretty URLs. 173 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 174 return redirect($this->listUrl($tree, (array) $request->getParsedBody())); 175 } 176 177 $params = $request->getQueryParams(); 178 $formats = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values(); 179 $go = $params['go'] ?? ''; 180 $page = (int) ($params['page'] ?? 1); 181 $max = (int) ($params['max'] ?? 20); 182 $folder = $params['folder'] ?? ''; 183 $filter = $params['filter'] ?? ''; 184 $subdirs = $params['subdirs'] ?? ''; 185 $format = $params['format'] ?? ''; 186 187 $folders = $this->allFolders($tree); 188 189 if ($go === '1') { 190 $media_objects = $this->allMedia( 191 $tree, 192 $folder, 193 $subdirs === '1' ? 'include' : 'exclude', 194 'title', 195 $filter, 196 $format 197 ); 198 } else { 199 $media_objects = new Collection(); 200 } 201 202 // Pagination 203 $count = $media_objects->count(); 204 $pages = (int) (($count + $max - 1) / $max); 205 $page = max(min($page, $pages), 1); 206 207 $media_objects = $media_objects->slice(($page - 1) * $max, $max); 208 209 return $this->viewResponse('modules/media-list/page', [ 210 'count' => $count, 211 'filter' => $filter, 212 'folder' => $folder, 213 'folders' => $folders, 214 'format' => $format, 215 'formats' => $formats, 216 'linked_record_service' => $this->linked_record_service, 217 'max' => $max, 218 'media_objects' => $media_objects, 219 'page' => $page, 220 'pages' => $pages, 221 'subdirs' => $subdirs, 222 'data_filesystem' => $data_filesystem, 223 'module' => $this, 224 'title' => I18N::translate('Media'), 225 'tree' => $tree, 226 ]); 227 } 228 229 /** 230 * Generate a list of all the folders in a current tree. 231 * 232 * @param Tree $tree 233 * 234 * @return array<string> 235 */ 236 private function allFolders(Tree $tree): array 237 { 238 $folders = DB::table('media_file') 239 ->where('m_file', '=', $tree->id()) 240 ->where('multimedia_file_refn', 'NOT LIKE', 'http:%') 241 ->where('multimedia_file_refn', 'NOT LIKE', 'https:%') 242 ->where('multimedia_file_refn', 'LIKE', '%/%') 243 ->pluck('multimedia_file_refn', 'multimedia_file_refn') 244 ->map(static function (string $path): string { 245 return dirname($path); 246 }) 247 ->uniqueStrict() 248 ->sort() 249 ->all(); 250 251 // Ensure we have an empty (top level) folder. 252 array_unshift($folders, ''); 253 254 return array_combine($folders, $folders); 255 } 256 257 /** 258 * Generate a list of all the media objects matching the criteria in a current tree. 259 * 260 * @param Tree $tree find media in this tree 261 * @param string $folder folder to search 262 * @param string $subfolders either "include" or "exclude" 263 * @param string $sort either "file" or "title" 264 * @param string $filter optional search string 265 * @param string $format option OBJE/FILE/FORM/TYPE 266 * 267 * @return Collection<int,Media> 268 */ 269 private function allMedia(Tree $tree, string $folder, string $subfolders, string $sort, string $filter, string $format): Collection 270 { 271 $query = DB::table('media') 272 ->join('media_file', static function (JoinClause $join): void { 273 $join 274 ->on('media_file.m_file', '=', 'media.m_file') 275 ->on('media_file.m_id', '=', 'media.m_id'); 276 }) 277 ->where('media.m_file', '=', $tree->id()); 278 279 if ($folder === '') { 280 // Include external URLs in the root folder. 281 if ($subfolders === 'exclude') { 282 $query->where(static function (Builder $query): void { 283 $query 284 ->where('multimedia_file_refn', 'NOT LIKE', '%/%') 285 ->orWhere('multimedia_file_refn', 'LIKE', 'http:%') 286 ->orWhere('multimedia_file_refn', 'LIKE', 'https:%'); 287 }); 288 } 289 } else { 290 // Exclude external URLs from the root folder. 291 $query 292 ->where('multimedia_file_refn', 'LIKE', $folder . '/%') 293 ->where('multimedia_file_refn', 'NOT LIKE', 'http:%') 294 ->where('multimedia_file_refn', 'NOT LIKE', 'https:%'); 295 296 if ($subfolders === 'exclude') { 297 $query->where('multimedia_file_refn', 'NOT LIKE', $folder . '/%/%'); 298 } 299 } 300 301 // Apply search terms 302 if ($filter !== '') { 303 $query->where(static function (Builder $query) use ($filter): void { 304 $like = '%' . addcslashes($filter, '\\%_') . '%'; 305 $query 306 ->where('multimedia_file_refn', 'LIKE', $like) 307 ->orWhere('descriptive_title', 'LIKE', $like); 308 }); 309 } 310 311 if ($format) { 312 $query->where('source_media_type', '=', $format); 313 } 314 315 switch ($sort) { 316 case 'file': 317 $query->orderBy('multimedia_file_refn'); 318 break; 319 case 'title': 320 $query->orderBy('descriptive_title'); 321 break; 322 } 323 324 return $query 325 ->get() 326 ->map(Registry::mediaFactory()->mapper($tree)) 327 ->uniqueStrict() 328 ->filter(GedcomRecord::accessFilter()); 329 } 330} 331