xref: /webtrees/app/Module/RepositoryListModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
667992b6aSRichard Cissee * This program is free software: you can redistribute it and/or modify
767992b6aSRichard Cissee * it under the terms of the GNU General Public License as published by
867992b6aSRichard Cissee * the Free Software Foundation, either version 3 of the License, or
967992b6aSRichard Cissee * (at your option) any later version.
1067992b6aSRichard Cissee * This program is distributed in the hope that it will be useful,
1167992b6aSRichard Cissee * but WITHOUT ANY WARRANTY; without even the implied warranty of
1267992b6aSRichard Cissee * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1367992b6aSRichard Cissee * GNU General Public License for more details.
1467992b6aSRichard Cissee * 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/>.
1667992b6aSRichard Cissee */
17fcfa147eSGreg Roach
1867992b6aSRichard Cisseedeclare(strict_types=1);
1967992b6aSRichard Cissee
2067992b6aSRichard Cisseenamespace Fisharebest\Webtrees\Module;
2167992b6aSRichard Cissee
22f0c88a96SGreg Roachuse Fisharebest\Webtrees\Auth;
23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
2406a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
26f0c88a96SGreg Roachuse Fisharebest\Webtrees\Registry;
27e72c24d6SGreg Roachuse Fisharebest\Webtrees\Repository;
2867992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
3157ab2231SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3206a438b4SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33f3874e19SGreg Roach
3467992b6aSRichard Cissee/**
3567992b6aSRichard Cissee * Class RepositoryListModule
3667992b6aSRichard Cissee */
3706a438b4SGreg Roachclass RepositoryListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
3867992b6aSRichard Cissee{
3967992b6aSRichard Cissee    use ModuleListTrait;
4067992b6aSRichard Cissee
4106a438b4SGreg Roach    protected const ROUTE_URL = '/tree/{tree}/repository-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_USER;
45e72c24d6SGreg Roach
4667992b6aSRichard Cissee    /**
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    /**
580cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
5967992b6aSRichard Cissee     *
6067992b6aSRichard Cissee     * @return string
6167992b6aSRichard Cissee     */
6267992b6aSRichard Cissee    public function title(): string
6367992b6aSRichard Cissee    {
6467992b6aSRichard Cissee        /* I18N: Name of a module/list */
6567992b6aSRichard Cissee        return I18N::translate('Repositories');
6667992b6aSRichard Cissee    }
6767992b6aSRichard Cissee
6867992b6aSRichard Cissee    public function description(): string
6967992b6aSRichard Cissee    {
70b5e8e56bSGreg Roach        /* I18N: Description of the “Repositories” module */
7167992b6aSRichard Cissee        return I18N::translate('A list of repositories.');
7267992b6aSRichard Cissee    }
7367992b6aSRichard Cissee
7467992b6aSRichard Cissee    /**
7567992b6aSRichard Cissee     * CSS class for the URL.
7667992b6aSRichard Cissee     *
7767992b6aSRichard Cissee     * @return string
7867992b6aSRichard Cissee     */
7967992b6aSRichard Cissee    public function listMenuClass(): string
8067992b6aSRichard Cissee    {
8167992b6aSRichard Cissee        return 'menu-list-repo';
8267992b6aSRichard Cissee    }
8367992b6aSRichard Cissee
844db4b4a9SGreg Roach    /**
8506a438b4SGreg Roach     * @param Tree                                      $tree
8676d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
874db4b4a9SGreg Roach     *
8806a438b4SGreg Roach     * @return string
894db4b4a9SGreg Roach     */
9006a438b4SGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
9167992b6aSRichard Cissee    {
9206a438b4SGreg Roach        $parameters['tree'] = $tree->name();
935229eadeSGreg Roach
9406a438b4SGreg Roach        return route(static::class, $parameters);
9567992b6aSRichard Cissee    }
9667992b6aSRichard Cissee
974db4b4a9SGreg Roach    /**
9824f2a3afSGreg Roach     * @return array<string>
994db4b4a9SGreg Roach     */
10067992b6aSRichard Cissee    public function listUrlAttributes(): array
10167992b6aSRichard Cissee    {
10267992b6aSRichard Cissee        return [];
10367992b6aSRichard Cissee    }
10467992b6aSRichard Cissee
1054db4b4a9SGreg Roach    /**
1064db4b4a9SGreg Roach     * @param Tree $tree
1074db4b4a9SGreg Roach     *
1084db4b4a9SGreg Roach     * @return bool
1094db4b4a9SGreg Roach     */
11067992b6aSRichard Cissee    public function listIsEmpty(Tree $tree): bool
11167992b6aSRichard Cissee    {
11267992b6aSRichard Cissee        return !DB::table('other')
11367992b6aSRichard Cissee            ->where('o_file', '=', $tree->id())
114e72c24d6SGreg Roach            ->where('o_type', '=', Repository::RECORD_TYPE)
11567992b6aSRichard Cissee            ->exists();
11667992b6aSRichard Cissee    }
11706a438b4SGreg Roach
11806a438b4SGreg Roach    /**
11906a438b4SGreg Roach     * @param ServerRequestInterface $request
12006a438b4SGreg Roach     *
12106a438b4SGreg Roach     * @return ResponseInterface
12206a438b4SGreg Roach     */
12306a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
12406a438b4SGreg Roach    {
125b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
126b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
12706a438b4SGreg Roach
12806a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
12906a438b4SGreg Roach
13006a438b4SGreg Roach        $repositories = DB::table('other')
13106a438b4SGreg Roach            ->where('o_file', '=', $tree->id())
13206a438b4SGreg Roach            ->where('o_type', '=', Repository::RECORD_TYPE)
13306a438b4SGreg Roach            ->get()
1346b9cb339SGreg Roach            ->map(Registry::repositoryFactory()->mapper($tree))
13506a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter());
13606a438b4SGreg Roach
13706a438b4SGreg Roach        return $this->viewResponse('modules/repository-list/page', [
13806a438b4SGreg Roach            'repositories' => $repositories,
13906a438b4SGreg Roach            'title'        => I18N::translate('Repositories'),
14006a438b4SGreg Roach            'tree'         => $tree,
14106a438b4SGreg Roach        ]);
14206a438b4SGreg Roach    }
14367992b6aSRichard Cissee}
144