xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentPage.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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 fn (Tree $tree): int => $module->accessLevel($tree, $interface))
73                    ->uniqueStrict()
74                    ->values()
75                    ->map(static fn (int $level): string => Auth::accessLevelNames()[$level])
76                    ->all();
77
78                return [$module->name() => $access_levels];
79            })
80            ->all();
81
82        return $this->viewResponse('admin/components', [
83            'description'    => $description,
84            'interface'      => $interface,
85            'modules'        => $modules,
86            'title'          => $title,
87            'trees'          => $this->tree_service->all(),
88            'uses_access'    => $uses_access,
89            'uses_sorting'   => $uses_sorting,
90            'access_summary' => $access_summary,
91        ]);
92    }
93}
94