xref: /webtrees/app/Module/RepositoryListModule.php (revision 28281361442225049c4409ca2916c9f1f2711269)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Repository;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\Validator;
29use Illuminate\Database\Capsule\Manager as DB;
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    /**
69     * A sentence describing what this module does.
70     *
71     * @return string
72     */
73    public function description(): string
74    {
75        /* I18N: Description of the “Repositories” module */
76        return I18N::translate('A list of repositories.');
77    }
78
79    /**
80     * CSS class for the URL.
81     *
82     * @return string
83     */
84    public function listMenuClass(): string
85    {
86        return 'menu-list-repo';
87    }
88
89    /**
90     * @param Tree                                      $tree
91     * @param array<bool|int|string|array<string>|null> $parameters
92     *
93     * @return string
94     */
95    public function listUrl(Tree $tree, array $parameters = []): string
96    {
97        $parameters['tree'] = $tree->name();
98
99        return route(static::class, $parameters);
100    }
101
102    /**
103     * @return array<string>
104     */
105    public function listUrlAttributes(): array
106    {
107        return [];
108    }
109
110    /**
111     * @param Tree $tree
112     *
113     * @return bool
114     */
115    public function listIsEmpty(Tree $tree): bool
116    {
117        return !DB::table('other')
118            ->where('o_file', '=', $tree->id())
119            ->where('o_type', '=', Repository::RECORD_TYPE)
120            ->exists();
121    }
122
123    /**
124     * @param ServerRequestInterface $request
125     *
126     * @return ResponseInterface
127     */
128    public function handle(ServerRequestInterface $request): ResponseInterface
129    {
130        $tree = Validator::attributes($request)->tree();
131        $user = Validator::attributes($request)->user();
132
133        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
134
135        $repositories = DB::table('other')
136            ->where('o_file', '=', $tree->id())
137            ->where('o_type', '=', Repository::RECORD_TYPE)
138            ->get()
139            ->map(Registry::repositoryFactory()->mapper($tree))
140            ->filter(GedcomRecord::accessFilter());
141
142        return $this->viewResponse('modules/repository-list/page', [
143            'repositories' => $repositories,
144            'title'        => I18N::translate('Repositories'),
145            'tree'         => $tree,
146        ]);
147    }
148}
149