xref: /webtrees/app/Module/SubmitterListModule.php (revision e873f434551745f888937263ff89e80db3b0f785)
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 string 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    public function description(): string
69    {
70        /* I18N: Description of the “Submitters” module */
71        return I18N::translate('A list of submitters.');
72    }
73
74    /**
75     * Should this module be enabled when it is first installed?
76     *
77     * @return bool
78     */
79    public function isEnabledByDefault(): bool
80    {
81        return false;
82    }
83
84    /**
85     * CSS class for the URL.
86     *
87     * @return string
88     */
89    public function listMenuClass(): string
90    {
91        return 'menu-list-subm';
92    }
93
94    /**
95     * @param Tree                                      $tree
96     * @param array<bool|int|string|array<string>|null> $parameters
97     *
98     * @return string
99     */
100    public function listUrl(Tree $tree, array $parameters = []): string
101    {
102        $parameters['tree'] = $tree->name();
103
104        return route(static::class, $parameters);
105    }
106
107    /**
108     * @return array<string>
109     */
110    public function listUrlAttributes(): array
111    {
112        return [];
113    }
114
115    /**
116     * @param Tree $tree
117     *
118     * @return bool
119     */
120    public function listIsEmpty(Tree $tree): bool
121    {
122        return !DB::table('other')
123            ->where('o_file', '=', $tree->id())
124            ->where('o_type', '=', Submitter::RECORD_TYPE)
125            ->exists();
126    }
127
128    /**
129     * @param ServerRequestInterface $request
130     *
131     * @return ResponseInterface
132     */
133    public function handle(ServerRequestInterface $request): ResponseInterface
134    {
135        $tree = Validator::attributes($request)->tree();
136        $user = Validator::attributes($request)->user();
137
138        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
139
140        $submitters = DB::table('other')
141            ->where('o_file', '=', $tree->id())
142            ->where('o_type', '=', Submitter::RECORD_TYPE)
143            ->get()
144            ->map(Registry::submitterFactory()->mapper($tree))
145            ->filter(GedcomRecord::accessFilter());
146
147        return $this->viewResponse('modules/submitter-list/page', [
148            'submitters'   => $submitters,
149            'title'        => I18N::translate('Submitters'),
150            'tree'         => $tree,
151        ]);
152    }
153}
154