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