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