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