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