xref: /webtrees/app/Module/SubmitterListModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1e72c24d6SGreg Roach<?php
2e72c24d6SGreg Roach
3e72c24d6SGreg Roach/**
4e72c24d6SGreg Roach * webtrees: online genealogy
5d11be702SGreg 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;
23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2406a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
25e72c24d6SGreg Roachuse Fisharebest\Webtrees\I18N;
2609482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
27e72c24d6SGreg Roachuse Fisharebest\Webtrees\Submitter;
28e72c24d6SGreg Roachuse Fisharebest\Webtrees\Tree;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
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    public function description(): string
69e72c24d6SGreg Roach    {
70f1c3484cSGreg Roach        /* I18N: Description of the “Submitters” module */
71e72c24d6SGreg Roach        return I18N::translate('A list of submitters.');
72e72c24d6SGreg Roach    }
73e72c24d6SGreg Roach
74e72c24d6SGreg Roach    /**
75e72c24d6SGreg Roach     * Should this module be enabled when it is first installed?
76e72c24d6SGreg Roach     *
77e72c24d6SGreg Roach     * @return bool
78e72c24d6SGreg Roach     */
79e72c24d6SGreg Roach    public function isEnabledByDefault(): bool
80e72c24d6SGreg Roach    {
81e72c24d6SGreg Roach        return false;
82e72c24d6SGreg Roach    }
83e72c24d6SGreg Roach
84e72c24d6SGreg Roach    /**
85e72c24d6SGreg Roach     * CSS class for the URL.
86e72c24d6SGreg Roach     *
87e72c24d6SGreg Roach     * @return string
88e72c24d6SGreg Roach     */
89e72c24d6SGreg Roach    public function listMenuClass(): string
90e72c24d6SGreg Roach    {
91e72c24d6SGreg Roach        return 'menu-list-subm';
92e72c24d6SGreg Roach    }
93e72c24d6SGreg Roach
94e72c24d6SGreg Roach    /**
9506a438b4SGreg Roach     * @param Tree                                      $tree
9676d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
97e72c24d6SGreg Roach     *
9806a438b4SGreg Roach     * @return string
99e72c24d6SGreg Roach     */
10006a438b4SGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
101e72c24d6SGreg Roach    {
10206a438b4SGreg Roach        $parameters['tree'] = $tree->name();
103e72c24d6SGreg Roach
10406a438b4SGreg Roach        return route(static::class, $parameters);
105e72c24d6SGreg Roach    }
106e72c24d6SGreg Roach
107e72c24d6SGreg Roach    /**
10824f2a3afSGreg Roach     * @return array<string>
109e72c24d6SGreg Roach     */
110e72c24d6SGreg Roach    public function listUrlAttributes(): array
111e72c24d6SGreg Roach    {
112e72c24d6SGreg Roach        return [];
113e72c24d6SGreg Roach    }
114e72c24d6SGreg Roach
115e72c24d6SGreg Roach    /**
116e72c24d6SGreg Roach     * @param Tree $tree
117e72c24d6SGreg Roach     *
118e72c24d6SGreg Roach     * @return bool
119e72c24d6SGreg Roach     */
120e72c24d6SGreg Roach    public function listIsEmpty(Tree $tree): bool
121e72c24d6SGreg Roach    {
122e72c24d6SGreg Roach        return !DB::table('other')
123e72c24d6SGreg Roach            ->where('o_file', '=', $tree->id())
124e72c24d6SGreg Roach            ->where('o_type', '=', Submitter::RECORD_TYPE)
125e72c24d6SGreg Roach            ->exists();
126e72c24d6SGreg Roach    }
12706a438b4SGreg Roach
12806a438b4SGreg Roach    /**
12906a438b4SGreg Roach     * @param ServerRequestInterface $request
13006a438b4SGreg Roach     *
13106a438b4SGreg Roach     * @return ResponseInterface
13206a438b4SGreg Roach     */
13306a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
13406a438b4SGreg Roach    {
135b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
136b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
13706a438b4SGreg Roach
13806a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
13906a438b4SGreg Roach
14006a438b4SGreg Roach        $submitters = DB::table('other')
14106a438b4SGreg Roach            ->where('o_file', '=', $tree->id())
14206a438b4SGreg Roach            ->where('o_type', '=', Submitter::RECORD_TYPE)
14306a438b4SGreg Roach            ->get()
1446b9cb339SGreg Roach            ->map(Registry::submitterFactory()->mapper($tree))
14506a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter());
14606a438b4SGreg Roach
14706a438b4SGreg Roach        return $this->viewResponse('modules/submitter-list/page', [
14806a438b4SGreg Roach            'submitters'   => $submitters,
14906a438b4SGreg Roach            'title'        => I18N::translate('Submitters'),
15006a438b4SGreg Roach            'tree'         => $tree,
15106a438b4SGreg Roach        ]);
15206a438b4SGreg Roach    }
153e72c24d6SGreg Roach}
154