xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentPage.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
19f667ff2SGreg Roach<?php
29f667ff2SGreg Roach
39f667ff2SGreg Roach/**
49f667ff2SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
69f667ff2SGreg Roach * This program is free software: you can redistribute it and/or modify
79f667ff2SGreg Roach * it under the terms of the GNU General Public License as published by
89f667ff2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
99f667ff2SGreg Roach * (at your option) any later version.
109f667ff2SGreg Roach * This program is distributed in the hope that it will be useful,
119f667ff2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
129f667ff2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139f667ff2SGreg Roach * GNU General Public License for more details.
149f667ff2SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
169f667ff2SGreg Roach */
179f667ff2SGreg Roach
189f667ff2SGreg Roachdeclare(strict_types=1);
199f667ff2SGreg Roach
209f667ff2SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
219f667ff2SGreg Roach
229f667ff2SGreg Roachuse Fisharebest\Webtrees\Auth;
239f667ff2SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
249f667ff2SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
259f667ff2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
269f667ff2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
274c78e066SGreg Roachuse Fisharebest\Webtrees\Tree;
289f667ff2SGreg Roachuse Psr\Http\Message\ResponseInterface;
299f667ff2SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
309f667ff2SGreg Roach
319f667ff2SGreg Roach/**
329f667ff2SGreg Roach * Show a list of modules.
339f667ff2SGreg Roach */
349f667ff2SGreg Roachabstract class AbstractModuleComponentPage implements RequestHandlerInterface
359f667ff2SGreg Roach{
369f667ff2SGreg Roach    use ViewResponseTrait;
379f667ff2SGreg Roach
38ac732901SGreg Roach    private ModuleService $module_service;
399f667ff2SGreg Roach
40ac732901SGreg Roach    private TreeService $tree_service;
419f667ff2SGreg Roach
429f667ff2SGreg Roach    /**
439f667ff2SGreg Roach     * @param ModuleService $module_service
449f667ff2SGreg Roach     * @param TreeService   $tree_service
459f667ff2SGreg Roach     */
469f667ff2SGreg Roach    public function __construct(ModuleService $module_service, TreeService $tree_service)
479f667ff2SGreg Roach    {
489f667ff2SGreg Roach        $this->module_service = $module_service;
499f667ff2SGreg Roach        $this->tree_service   = $tree_service;
509f667ff2SGreg Roach    }
519f667ff2SGreg Roach
529f667ff2SGreg Roach    /**
53ac19d600SGreg Roach     * @template T of ModuleInterface
54ac19d600SGreg Roach     *
55ac19d600SGreg Roach     * @param class-string<T> $interface
569f667ff2SGreg Roach     * @param string          $title
579f667ff2SGreg Roach     * @param string          $description
589f667ff2SGreg Roach     *
599f667ff2SGreg Roach     * @return ResponseInterface
609f667ff2SGreg Roach     */
619f667ff2SGreg Roach    protected function listComponents(string $interface, string $title, string $description): ResponseInterface
629f667ff2SGreg Roach    {
639f667ff2SGreg Roach        $this->layout = 'layouts/administration';
649f667ff2SGreg Roach
659f667ff2SGreg Roach        $modules      = $this->module_service->findByInterface($interface, true, true);
669f667ff2SGreg Roach        $uses_access  = $this->module_service->componentsWithAccess()->containsStrict($interface);
679f667ff2SGreg Roach        $uses_sorting = $this->module_service->componentsWithOrder()->containsStrict($interface);
689f667ff2SGreg Roach
699f667ff2SGreg Roach        $access_summary = $modules
709f667ff2SGreg Roach            ->mapWithKeys(function (ModuleInterface $module) use ($interface): array {
719f667ff2SGreg Roach                $access_levels = $this->tree_service->all()
72*f25fc0f9SGreg Roach                    ->map(static fn (Tree $tree): int => $module->accessLevel($tree, $interface))
739f667ff2SGreg Roach                    ->uniqueStrict()
749f667ff2SGreg Roach                    ->values()
75*f25fc0f9SGreg Roach                    ->map(static fn (int $level): string => Auth::accessLevelNames()[$level])
769f667ff2SGreg Roach                    ->all();
779f667ff2SGreg Roach
789f667ff2SGreg Roach                return [$module->name() => $access_levels];
799f667ff2SGreg Roach            })
809f667ff2SGreg Roach            ->all();
819f667ff2SGreg Roach
829f667ff2SGreg Roach        return $this->viewResponse('admin/components', [
839f667ff2SGreg Roach            'description'    => $description,
849f667ff2SGreg Roach            'interface'      => $interface,
859f667ff2SGreg Roach            'modules'        => $modules,
869f667ff2SGreg Roach            'title'          => $title,
879f667ff2SGreg Roach            'trees'          => $this->tree_service->all(),
889f667ff2SGreg Roach            'uses_access'    => $uses_access,
899f667ff2SGreg Roach            'uses_sorting'   => $uses_sorting,
909f667ff2SGreg Roach            'access_summary' => $access_summary,
919f667ff2SGreg Roach        ]);
929f667ff2SGreg Roach    }
939f667ff2SGreg Roach}
94