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