xref: /webtrees/app/Http/RequestHandlers/AbstractModuleComponentPage.php (revision d11be7027e34e3121be11cc025421873364403f9)
19f667ff2SGreg Roach<?php
29f667ff2SGreg Roach
39f667ff2SGreg Roach/**
49f667ff2SGreg Roach * webtrees: online genealogy
5*d11be702SGreg 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    /**
539f667ff2SGreg Roach     * @param string $interface
549f667ff2SGreg Roach     * @param string $title
559f667ff2SGreg Roach     * @param string $description
569f667ff2SGreg Roach     *
579f667ff2SGreg Roach     * @return ResponseInterface
589f667ff2SGreg Roach     */
599f667ff2SGreg Roach    protected function listComponents(string $interface, string $title, string $description): ResponseInterface
609f667ff2SGreg Roach    {
619f667ff2SGreg Roach        $this->layout = 'layouts/administration';
629f667ff2SGreg Roach
639f667ff2SGreg Roach        $modules      = $this->module_service->findByInterface($interface, true, true);
649f667ff2SGreg Roach        $uses_access  = $this->module_service->componentsWithAccess()->containsStrict($interface);
659f667ff2SGreg Roach        $uses_sorting = $this->module_service->componentsWithOrder()->containsStrict($interface);
669f667ff2SGreg Roach
679f667ff2SGreg Roach        $access_summary = $modules
689f667ff2SGreg Roach            ->mapWithKeys(function (ModuleInterface $module) use ($interface): array {
699f667ff2SGreg Roach                $access_levels = $this->tree_service->all()
704c78e066SGreg Roach                    ->map(static function (Tree $tree) use ($interface, $module): int {
719f667ff2SGreg Roach                        return $module->accessLevel($tree, $interface);
729f667ff2SGreg Roach                    })
739f667ff2SGreg Roach                    ->uniqueStrict()
749f667ff2SGreg Roach                    ->values()
759f667ff2SGreg Roach                    ->map(static function (int $level): string {
769f667ff2SGreg Roach                        return Auth::accessLevelNames()[$level];
779f667ff2SGreg Roach                    })
789f667ff2SGreg Roach                    ->all();
799f667ff2SGreg Roach
809f667ff2SGreg Roach                return [$module->name() => $access_levels];
819f667ff2SGreg Roach            })
829f667ff2SGreg Roach            ->all();
839f667ff2SGreg Roach
849f667ff2SGreg Roach        return $this->viewResponse('admin/components', [
859f667ff2SGreg Roach            'description'    => $description,
869f667ff2SGreg Roach            'interface'      => $interface,
879f667ff2SGreg Roach            'modules'        => $modules,
889f667ff2SGreg Roach            'title'          => $title,
899f667ff2SGreg Roach            'trees'          => $this->tree_service->all(),
909f667ff2SGreg Roach            'uses_access'    => $uses_access,
919f667ff2SGreg Roach            'uses_sorting'   => $uses_sorting,
929f667ff2SGreg Roach            'access_summary' => $access_summary,
939f667ff2SGreg Roach        ]);
949f667ff2SGreg Roach    }
959f667ff2SGreg Roach}
96