xref: /webtrees/app/Module/SubmitterListModule.php (revision d11be7027e34e3121be11cc025421873364403f9)
1e72c24d6SGreg Roach<?php
2e72c24d6SGreg Roach
3e72c24d6SGreg Roach/**
4e72c24d6SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6e72c24d6SGreg Roach * This program is free software: you can redistribute it and/or modify
7e72c24d6SGreg Roach * it under the terms of the GNU General Public License as published by
8e72c24d6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e72c24d6SGreg Roach * (at your option) any later version.
10e72c24d6SGreg Roach * This program is distributed in the hope that it will be useful,
11e72c24d6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e72c24d6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e72c24d6SGreg Roach * GNU General Public License for more details.
14e72c24d6SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e72c24d6SGreg Roach */
17e72c24d6SGreg Roach
18e72c24d6SGreg Roachdeclare(strict_types=1);
19e72c24d6SGreg Roach
20e72c24d6SGreg Roachnamespace Fisharebest\Webtrees\Module;
21e72c24d6SGreg Roach
2209482a55SGreg Roachuse Fisharebest\Webtrees\Auth;
2306a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
24e72c24d6SGreg Roachuse Fisharebest\Webtrees\I18N;
2509482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
26e72c24d6SGreg Roachuse Fisharebest\Webtrees\Submitter;
27e72c24d6SGreg Roachuse Fisharebest\Webtrees\Tree;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29e72c24d6SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
30e72c24d6SGreg Roachuse Psr\Http\Message\ResponseInterface;
31e72c24d6SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3206a438b4SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33e72c24d6SGreg Roach
34e72c24d6SGreg Roach/**
35e72c24d6SGreg Roach * Class SubmitterListModule
36e72c24d6SGreg Roach */
3706a438b4SGreg Roachclass SubmitterListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
38e72c24d6SGreg Roach{
39e72c24d6SGreg Roach    use ModuleListTrait;
40e72c24d6SGreg Roach
4106a438b4SGreg Roach    protected const ROUTE_URL = '/tree/{tree}/submitter-list';
4206a438b4SGreg Roach
43e72c24d6SGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
4433c746f1SGreg Roach    protected int $access_level = Auth::PRIV_NONE;
45e72c24d6SGreg Roach
46e72c24d6SGreg Roach    /**
4706a438b4SGreg Roach     * Initialization.
4806a438b4SGreg Roach     *
4906a438b4SGreg Roach     * @return void
5006a438b4SGreg Roach     */
5106a438b4SGreg Roach    public function boot(): void
5206a438b4SGreg Roach    {
53158900c2SGreg Roach        Registry::routeFactory()->routeMap()
5406a438b4SGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
5506a438b4SGreg Roach    }
5606a438b4SGreg Roach
5706a438b4SGreg Roach    /**
58e72c24d6SGreg Roach     * How should this module be identified in the control panel, etc.?
59e72c24d6SGreg Roach     *
60e72c24d6SGreg Roach     * @return string
61e72c24d6SGreg Roach     */
62e72c24d6SGreg Roach    public function title(): string
63e72c24d6SGreg Roach    {
64e72c24d6SGreg Roach        /* I18N: Name of a module/list */
65e72c24d6SGreg Roach        return I18N::translate('Submitters');
66e72c24d6SGreg Roach    }
67e72c24d6SGreg Roach
68e72c24d6SGreg Roach    /**
69e72c24d6SGreg Roach     * A sentence describing what this module does.
70e72c24d6SGreg Roach     *
71e72c24d6SGreg Roach     * @return string
72e72c24d6SGreg Roach     */
73e72c24d6SGreg Roach    public function description(): string
74e72c24d6SGreg Roach    {
75f1c3484cSGreg Roach        /* I18N: Description of the “Submitters” module */
76e72c24d6SGreg Roach        return I18N::translate('A list of submitters.');
77e72c24d6SGreg Roach    }
78e72c24d6SGreg Roach
79e72c24d6SGreg Roach    /**
80e72c24d6SGreg Roach     * Should this module be enabled when it is first installed?
81e72c24d6SGreg Roach     *
82e72c24d6SGreg Roach     * @return bool
83e72c24d6SGreg Roach     */
84e72c24d6SGreg Roach    public function isEnabledByDefault(): bool
85e72c24d6SGreg Roach    {
86e72c24d6SGreg Roach        return false;
87e72c24d6SGreg Roach    }
88e72c24d6SGreg Roach
89e72c24d6SGreg Roach    /**
90e72c24d6SGreg Roach     * CSS class for the URL.
91e72c24d6SGreg Roach     *
92e72c24d6SGreg Roach     * @return string
93e72c24d6SGreg Roach     */
94e72c24d6SGreg Roach    public function listMenuClass(): string
95e72c24d6SGreg Roach    {
96e72c24d6SGreg Roach        return 'menu-list-subm';
97e72c24d6SGreg Roach    }
98e72c24d6SGreg Roach
99e72c24d6SGreg Roach    /**
10006a438b4SGreg Roach     * @param Tree                                      $tree
10176d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
102e72c24d6SGreg Roach     *
10306a438b4SGreg Roach     * @return string
104e72c24d6SGreg Roach     */
10506a438b4SGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
106e72c24d6SGreg Roach    {
10706a438b4SGreg Roach        $parameters['tree'] = $tree->name();
108e72c24d6SGreg Roach
10906a438b4SGreg Roach        return route(static::class, $parameters);
110e72c24d6SGreg Roach    }
111e72c24d6SGreg Roach
112e72c24d6SGreg Roach    /**
11324f2a3afSGreg Roach     * @return array<string>
114e72c24d6SGreg Roach     */
115e72c24d6SGreg Roach    public function listUrlAttributes(): array
116e72c24d6SGreg Roach    {
117e72c24d6SGreg Roach        return [];
118e72c24d6SGreg Roach    }
119e72c24d6SGreg Roach
120e72c24d6SGreg Roach    /**
121e72c24d6SGreg Roach     * @param Tree $tree
122e72c24d6SGreg Roach     *
123e72c24d6SGreg Roach     * @return bool
124e72c24d6SGreg Roach     */
125e72c24d6SGreg Roach    public function listIsEmpty(Tree $tree): bool
126e72c24d6SGreg Roach    {
127e72c24d6SGreg Roach        return !DB::table('other')
128e72c24d6SGreg Roach            ->where('o_file', '=', $tree->id())
129e72c24d6SGreg Roach            ->where('o_type', '=', Submitter::RECORD_TYPE)
130e72c24d6SGreg Roach            ->exists();
131e72c24d6SGreg Roach    }
13206a438b4SGreg Roach
13306a438b4SGreg Roach    /**
13406a438b4SGreg Roach     * @param ServerRequestInterface $request
13506a438b4SGreg Roach     *
13606a438b4SGreg Roach     * @return ResponseInterface
13706a438b4SGreg Roach     */
13806a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
13906a438b4SGreg Roach    {
140b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
141b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
14206a438b4SGreg Roach
14306a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
14406a438b4SGreg Roach
14506a438b4SGreg Roach        $submitters = DB::table('other')
14606a438b4SGreg Roach            ->where('o_file', '=', $tree->id())
14706a438b4SGreg Roach            ->where('o_type', '=', Submitter::RECORD_TYPE)
14806a438b4SGreg Roach            ->get()
1496b9cb339SGreg Roach            ->map(Registry::submitterFactory()->mapper($tree))
15006a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter());
15106a438b4SGreg Roach
15206a438b4SGreg Roach        return $this->viewResponse('modules/submitter-list/page', [
15306a438b4SGreg Roach            'submitters'   => $submitters,
15406a438b4SGreg Roach            'title'        => I18N::translate('Submitters'),
15506a438b4SGreg Roach            'tree'         => $tree,
15606a438b4SGreg Roach        ]);
15706a438b4SGreg Roach    }
158e72c24d6SGreg Roach}
159