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