xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentPage.php (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
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     * @param string $interface
54     * @param string $title
55     * @param string $description
56     *
57     * @return ResponseInterface
58     */
59    protected function listComponents(string $interface, string $title, string $description): ResponseInterface
60    {
61        $this->layout = 'layouts/administration';
62
63        $modules      = $this->module_service->findByInterface($interface, true, true);
64        $uses_access  = $this->module_service->componentsWithAccess()->containsStrict($interface);
65        $uses_sorting = $this->module_service->componentsWithOrder()->containsStrict($interface);
66
67        $access_summary = $modules
68            ->mapWithKeys(function (ModuleInterface $module) use ($interface): array {
69                $access_levels = $this->tree_service->all()
70                    ->map(static function (Tree $tree) use ($interface, $module): int {
71                        return $module->accessLevel($tree, $interface);
72                    })
73                    ->uniqueStrict()
74                    ->values()
75                    ->map(static function (int $level): string {
76                        return Auth::accessLevelNames()[$level];
77                    })
78                    ->all();
79
80                return [$module->name() => $access_levels];
81            })
82            ->all();
83
84        return $this->viewResponse('admin/components', [
85            'description'    => $description,
86            'interface'      => $interface,
87            'modules'        => $modules,
88            'title'          => $title,
89            'trees'          => $this->tree_service->all(),
90            'uses_access'    => $uses_access,
91            'uses_sorting'   => $uses_sorting,
92            'access_summary' => $access_summary,
93        ]);
94    }
95}
96