xref: /webtrees/app/Module/SourceListModule.php (revision b95649eb604c07d7da487266940a09329a0824f5)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Aura\Router\RouterContainer;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\GedcomRecord;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\Auth;
29use Illuminate\Database\Capsule\Manager as DB;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function app;
35use function assert;
36use function redirect;
37
38/**
39 * Class RepositoryListModule
40 */
41class SourceListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
42{
43    use ModuleListTrait;
44
45    protected const ROUTE_URL = '/tree/{tree}/source-list';
46
47    /** @var int The default access level for this module.  It can be changed in the control panel. */
48    protected $access_level = Auth::PRIV_USER;
49
50    /**
51     * Initialization.
52     *
53     * @return void
54     */
55    public function boot(): void
56    {
57        $router_container = app(RouterContainer::class);
58        assert($router_container instanceof RouterContainer);
59
60        $router_container->getMap()
61            ->get(static::class, static::ROUTE_URL, $this);
62    }
63
64    /**
65     * How should this module be identified in the control panel, etc.?
66     *
67     * @return string
68     */
69    public function title(): string
70    {
71        /* I18N: Name of a module/list */
72        return I18N::translate('Sources');
73    }
74
75    /**
76     * A sentence describing what this module does.
77     *
78     * @return string
79     */
80    public function description(): string
81    {
82        /* I18N: Description of the “Sources” module */
83        return I18N::translate('A list of sources.');
84    }
85
86    /**
87     * CSS class for the URL.
88     *
89     * @return string
90     */
91    public function listMenuClass(): string
92    {
93        return 'menu-list-sour';
94    }
95
96    /**
97     * @param Tree    $tree
98     * @param mixed[] $parameters
99     *
100     * @return string
101     */
102    public function listUrl(Tree $tree, array $parameters = []): string
103    {
104        $parameters['tree'] = $tree->name();
105
106        return route(static::class, $parameters);
107    }
108
109    /**
110     * @return string[]
111     */
112    public function listUrlAttributes(): array
113    {
114        return [];
115    }
116
117    /**
118     * @param Tree $tree
119     *
120     * @return bool
121     */
122    public function listIsEmpty(Tree $tree): bool
123    {
124        return !DB::table('sources')
125            ->where('s_file', '=', $tree->id())
126            ->exists();
127    }
128
129    /**
130     * Handle URLs generated by older versions of webtrees
131     *
132     * @param ServerRequestInterface $request
133     *
134     * @return ResponseInterface
135     */
136    public function getListAction(ServerRequestInterface $request): ResponseInterface
137    {
138        return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams()));
139    }
140
141    /**
142     * @param ServerRequestInterface $request
143     *
144     * @return ResponseInterface
145     */
146    public function handle(ServerRequestInterface $request): ResponseInterface
147    {
148        $tree = $request->getAttribute('tree');
149        assert($tree instanceof Tree);
150
151        $user = $request->getAttribute('user');
152        assert($user instanceof UserInterface);
153
154        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
155
156        $sources = DB::table('sources')
157            ->where('s_file', '=', $tree->id())
158            ->get()
159            ->map(Registry::sourceFactory()->mapper($tree))
160            ->filter(GedcomRecord::accessFilter());
161
162        return $this->viewResponse('modules/source-list/page', [
163            'sources'   => $sources,
164            'title'     => I18N::translate('Sources'),
165            'tree'      => $tree,
166        ]);
167    }
168}
169