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