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