xref: /webtrees/app/Module/RepositoryListModule.php (revision 9d74ed0ab3ae61aa8ae07578e026f14020844ce9)
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\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\DB;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Repository;
28use Fisharebest\Webtrees\Tree;
29use Fisharebest\Webtrees\Validator;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34/**
35 * Class RepositoryListModule
36 */
37class RepositoryListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
38{
39    use ModuleListTrait;
40
41    protected const ROUTE_URL = '/tree/{tree}/repository-list';
42
43    /** @var int The default access level for this module.  It can be changed in the control panel. */
44    protected int $access_level = Auth::PRIV_USER;
45
46    /**
47     * Initialization.
48     *
49     * @return void
50     */
51    public function boot(): void
52    {
53        Registry::routeFactory()->routeMap()
54            ->get(static::class, static::ROUTE_URL, $this);
55    }
56
57    /**
58     * How should this module be identified in the control panel, etc.?
59     *
60     * @return string
61     */
62    public function title(): string
63    {
64        /* I18N: Name of a module/list */
65        return I18N::translate('Repositories');
66    }
67
68    public function description(): string
69    {
70        /* I18N: Description of the “Repositories” module */
71        return I18N::translate('A list of repositories.');
72    }
73
74    /**
75     * CSS class for the URL.
76     *
77     * @return string
78     */
79    public function listMenuClass(): string
80    {
81        return 'menu-list-repo';
82    }
83
84    /**
85     * @param Tree                                      $tree
86     * @param array<bool|int|string|array<string>|null> $parameters
87     *
88     * @return string
89     */
90    public function listUrl(Tree $tree, array $parameters = []): string
91    {
92        $parameters['tree'] = $tree->name();
93
94        return route(static::class, $parameters);
95    }
96
97    /**
98     * @return array<string>
99     */
100    public function listUrlAttributes(): array
101    {
102        return [];
103    }
104
105    /**
106     * @param Tree $tree
107     *
108     * @return bool
109     */
110    public function listIsEmpty(Tree $tree): bool
111    {
112        return !DB::table('other')
113            ->where('o_file', '=', $tree->id())
114            ->where('o_type', '=', Repository::RECORD_TYPE)
115            ->exists();
116    }
117
118    /**
119     * @param ServerRequestInterface $request
120     *
121     * @return ResponseInterface
122     */
123    public function handle(ServerRequestInterface $request): ResponseInterface
124    {
125        $tree = Validator::attributes($request)->tree();
126        $user = Validator::attributes($request)->user();
127
128        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
129
130        $repositories = DB::table('other')
131            ->where('o_file', '=', $tree->id())
132            ->where('o_type', '=', Repository::RECORD_TYPE)
133            ->get()
134            ->map(Registry::repositoryFactory()->mapper($tree))
135            ->filter(GedcomRecord::accessFilter());
136
137        return $this->viewResponse('modules/repository-list/page', [
138            'repositories' => $repositories,
139            'title'        => I18N::translate('Repositories'),
140            'tree'         => $tree,
141        ]);
142    }
143}
144