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