1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Http\ViewResponseTrait; 24use Fisharebest\Webtrees\Module\ModuleInterface; 25use Fisharebest\Webtrees\Services\ModuleService; 26use Fisharebest\Webtrees\Services\TreeService; 27use Fisharebest\Webtrees\Tree; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Server\RequestHandlerInterface; 30 31/** 32 * Show a list of modules. 33 */ 34abstract class AbstractModuleComponentPage implements RequestHandlerInterface 35{ 36 use ViewResponseTrait; 37 38 private ModuleService $module_service; 39 40 private TreeService $tree_service; 41 42 /** 43 * @param ModuleService $module_service 44 * @param TreeService $tree_service 45 */ 46 public function __construct(ModuleService $module_service, TreeService $tree_service) 47 { 48 $this->module_service = $module_service; 49 $this->tree_service = $tree_service; 50 } 51 52 /** 53 * @template T of ModuleInterface 54 * 55 * @param class-string<T> $interface 56 * @param string $title 57 * @param string $description 58 * 59 * @return ResponseInterface 60 */ 61 protected function listComponents(string $interface, string $title, string $description): ResponseInterface 62 { 63 $this->layout = 'layouts/administration'; 64 65 $modules = $this->module_service->findByInterface($interface, true, true); 66 $uses_access = $this->module_service->componentsWithAccess()->containsStrict($interface); 67 $uses_sorting = $this->module_service->componentsWithOrder()->containsStrict($interface); 68 69 $access_summary = $modules 70 ->mapWithKeys(function (ModuleInterface $module) use ($interface): array { 71 $access_levels = $this->tree_service->all() 72 ->map(static function (Tree $tree) use ($interface, $module): int { 73 return $module->accessLevel($tree, $interface); 74 }) 75 ->uniqueStrict() 76 ->values() 77 ->map(static function (int $level): string { 78 return Auth::accessLevelNames()[$level]; 79 }) 80 ->all(); 81 82 return [$module->name() => $access_levels]; 83 }) 84 ->all(); 85 86 return $this->viewResponse('admin/components', [ 87 'description' => $description, 88 'interface' => $interface, 89 'modules' => $modules, 90 'title' => $title, 91 'trees' => $this->tree_service->all(), 92 'uses_access' => $uses_access, 93 'uses_sorting' => $uses_sorting, 94 'access_summary' => $access_summary, 95 ]); 96 } 97} 98