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 Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Tree; 27use Fisharebest\Webtrees\Validator; 28use Illuminate\Database\Capsule\Manager as DB; 29use Psr\Http\Message\ResponseInterface; 30use Psr\Http\Message\ServerRequestInterface; 31use Psr\Http\Server\RequestHandlerInterface; 32 33use function redirect; 34 35/** 36 * Class RepositoryListModule 37 */ 38class SourceListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface 39{ 40 use ModuleListTrait; 41 42 protected const ROUTE_URL = '/tree/{tree}/source-list'; 43 44 /** @var int The default access level for this module. It can be changed in the control panel. */ 45 protected int $access_level = Auth::PRIV_USER; 46 47 /** 48 * Initialization. 49 * 50 * @return void 51 */ 52 public function boot(): void 53 { 54 Registry::routeFactory()->routeMap() 55 ->get(static::class, static::ROUTE_URL, $this); 56 } 57 58 /** 59 * How should this module be identified in the control panel, etc.? 60 * 61 * @return string 62 */ 63 public function title(): string 64 { 65 /* I18N: Name of a module/list */ 66 return I18N::translate('Sources'); 67 } 68 69 /** 70 * A sentence describing what this module does. 71 * 72 * @return string 73 */ 74 public function description(): string 75 { 76 /* I18N: Description of the “Sources” module */ 77 return I18N::translate('A list of sources.'); 78 } 79 80 /** 81 * CSS class for the URL. 82 * 83 * @return string 84 */ 85 public function listMenuClass(): string 86 { 87 return 'menu-list-sour'; 88 } 89 90 /** 91 * @param Tree $tree 92 * @param array<bool|int|string|array<string>|null> $parameters 93 * 94 * @return string 95 */ 96 public function listUrl(Tree $tree, array $parameters = []): string 97 { 98 $parameters['tree'] = $tree->name(); 99 100 return route(static::class, $parameters); 101 } 102 103 /** 104 * @return array<string> 105 */ 106 public function listUrlAttributes(): array 107 { 108 return []; 109 } 110 111 /** 112 * @param Tree $tree 113 * 114 * @return bool 115 */ 116 public function listIsEmpty(Tree $tree): bool 117 { 118 return !DB::table('sources') 119 ->where('s_file', '=', $tree->id()) 120 ->exists(); 121 } 122 123 /** 124 * Handle URLs generated by older versions of webtrees 125 * 126 * @param ServerRequestInterface $request 127 * 128 * @return ResponseInterface 129 */ 130 public function getListAction(ServerRequestInterface $request): ResponseInterface 131 { 132 $tree = Validator::attributes($request)->tree(); 133 134 return redirect($this->listUrl($tree, $request->getQueryParams())); 135 } 136 137 /** 138 * @param ServerRequestInterface $request 139 * 140 * @return ResponseInterface 141 */ 142 public function handle(ServerRequestInterface $request): ResponseInterface 143 { 144 $tree = Validator::attributes($request)->tree(); 145 $user = Validator::attributes($request)->user(); 146 147 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 148 149 $sources = DB::table('sources') 150 ->where('s_file', '=', $tree->id()) 151 ->get() 152 ->map(Registry::sourceFactory()->mapper($tree)) 153 ->filter(GedcomRecord::accessFilter()); 154 155 return $this->viewResponse('modules/source-list/page', [ 156 'sources' => $sources, 157 'title' => I18N::translate('Sources'), 158 'tree' => $tree, 159 ]); 160 } 161} 162