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 * @param ServerRequestInterface $request 146 * 147 * @return ResponseInterface 148 */ 149 public function handle(ServerRequestInterface $request): ResponseInterface 150 { 151 $tree = Validator::attributes($request)->tree(); 152 $user = Validator::attributes($request)->user(); 153 154 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 155 156 $formats = Registry::elementFactory()->make('OBJE:FILE:FORM:TYPE')->values(); 157 158 // Convert POST requests into GET requests for pretty URLs. 159 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 160 $params = [ 161 'go' => true, 162 'page' => Validator::parsedBody($request)->integer('page'), 163 'max' => Validator::parsedBody($request)->integer('max'), 164 'folder' => Validator::parsedBody($request)->string('folder'), 165 'filter' => Validator::parsedBody($request)->string('filter'), 166 'subdirs' => Validator::parsedBody($request)->boolean('subdirs', false), 167 'format' => Validator::parsedBody($request)->isInArrayKeys($formats)->string('format'), 168 ]; 169 170 return redirect($this->listUrl($tree, $params)); 171 } 172 173 $data_filesystem = Registry::filesystem()->data(); 174 $folders = $this->allFolders($tree); 175 176 $go = Validator::queryParams($request)->boolean('go', false); 177 $page = Validator::queryParams($request)->integer('page', 1); 178 $max = Validator::queryParams($request)->integer('page', 20); 179 $folder = Validator::queryParams($request)->string('folder', ''); 180 $filter = Validator::queryParams($request)->string('filter', ''); 181 $subdirs = Validator::queryParams($request)->boolean('subdirs', false); 182 $format = Validator::queryParams($request)->isInArrayKeys($formats)->string('format', ''); 183 184 if ($go) { 185 $media_objects = $this->allMedia($tree, $folder, $subdirs, 'title', $filter, $format); 186 } else { 187 $media_objects = new Collection(); 188 } 189 190 // Pagination 191 $count = $media_objects->count(); 192 $pages = (int) (($count + $max - 1) / $max); 193 $page = max(min($page, $pages), 1); 194 195 $media_objects = $media_objects->slice(($page - 1) * $max, $max); 196 197 return $this->viewResponse('modules/media-list/page', [ 198 'count' => $count, 199 'filter' => $filter, 200 'folder' => $folder, 201 'folders' => $folders, 202 'format' => $format, 203 'formats' => $formats, 204 'linked_record_service' => $this->linked_record_service, 205 'max' => $max, 206 'media_objects' => $media_objects, 207 'page' => $page, 208 'pages' => $pages, 209 'subdirs' => $subdirs, 210 'data_filesystem' => $data_filesystem, 211 'module' => $this, 212 'title' => I18N::translate('Media'), 213 'tree' => $tree, 214 ]); 215 } 216 217 /** 218 * Generate a list of all the folders in a current tree. 219 * 220 * @param Tree $tree 221 * 222 * @return array<string> 223 */ 224 private function allFolders(Tree $tree): array 225 { 226 $folders = DB::table('media_file') 227 ->where('m_file', '=', $tree->id()) 228 ->where('multimedia_file_refn', 'NOT LIKE', 'http:%') 229 ->where('multimedia_file_refn', 'NOT LIKE', 'https:%') 230 ->where('multimedia_file_refn', 'LIKE', '%/%') 231 ->pluck('multimedia_file_refn', 'multimedia_file_refn') 232 ->map(static function (string $path): string { 233 return dirname($path); 234 }) 235 ->uniqueStrict() 236 ->sort() 237 ->all(); 238 239 // Ensure we have an empty (top level) folder. 240 array_unshift($folders, ''); 241 242 return array_combine($folders, $folders); 243 } 244 245 /** 246 * Generate a list of all the media objects matching the criteria in a current tree. 247 * 248 * @param Tree $tree find media in this tree 249 * @param string $folder folder to search 250 * @param bool $subfolders 251 * @param string $sort either "file" or "title" 252 * @param string $filter optional search string 253 * @param string $format option OBJE/FILE/FORM/TYPE 254 * 255 * @return Collection<int,Media> 256 */ 257 private function allMedia(Tree $tree, string $folder, bool $subfolders, string $sort, string $filter, string $format): Collection 258 { 259 $query = DB::table('media') 260 ->join('media_file', static function (JoinClause $join): void { 261 $join 262 ->on('media_file.m_file', '=', 'media.m_file') 263 ->on('media_file.m_id', '=', 'media.m_id'); 264 }) 265 ->where('media.m_file', '=', $tree->id()); 266 267 if ($folder === '') { 268 // Include external URLs in the root folder. 269 if (!$subfolders) { 270 $query->where(static function (Builder $query): void { 271 $query 272 ->where('multimedia_file_refn', 'NOT LIKE', '%/%') 273 ->orWhere('multimedia_file_refn', 'LIKE', 'http:%') 274 ->orWhere('multimedia_file_refn', 'LIKE', 'https:%'); 275 }); 276 } 277 } else { 278 // Exclude external URLs from the root folder. 279 $query 280 ->where('multimedia_file_refn', 'LIKE', $folder . '/%') 281 ->where('multimedia_file_refn', 'NOT LIKE', 'http:%') 282 ->where('multimedia_file_refn', 'NOT LIKE', 'https:%'); 283 284 if (!$subfolders) { 285 $query->where('multimedia_file_refn', 'NOT LIKE', $folder . '/%/%'); 286 } 287 } 288 289 // Apply search terms 290 if ($filter !== '') { 291 $query->where(static function (Builder $query) use ($filter): void { 292 $like = '%' . addcslashes($filter, '\\%_') . '%'; 293 $query 294 ->where('multimedia_file_refn', 'LIKE', $like) 295 ->orWhere('descriptive_title', 'LIKE', $like); 296 }); 297 } 298 299 if ($format) { 300 $query->where('source_media_type', '=', $format); 301 } 302 303 switch ($sort) { 304 case 'file': 305 $query->orderBy('multimedia_file_refn'); 306 break; 307 case 'title': 308 $query->orderBy('descriptive_title'); 309 break; 310 } 311 312 return $query 313 ->get() 314 ->map(Registry::mediaFactory()->mapper($tree)) 315 ->uniqueStrict() 316 ->filter(GedcomRecord::accessFilter()); 317 } 318} 319