xref: /webtrees/app/Module/SourceListModule.php (revision 158900c2b212361851fcf0964e99084458ce1b3a)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
2206a438b4SGreg Roachuse Aura\Router\RouterContainer;
2309482a55SGreg Roachuse Fisharebest\Webtrees\Auth;
2406a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
2609482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
2767992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2967992b6aSRichard Cisseeuse Illuminate\Database\Capsule\Manager as DB;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
3157ab2231SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3206a438b4SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33f3874e19SGreg Roach
3406a438b4SGreg Roachuse function app;
355229eadeSGreg Roachuse function assert;
3606a438b4SGreg Roachuse function redirect;
3767992b6aSRichard Cissee
3867992b6aSRichard Cissee/**
3967992b6aSRichard Cissee * Class RepositoryListModule
4067992b6aSRichard Cissee */
4106a438b4SGreg Roachclass SourceListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
4267992b6aSRichard Cissee{
4367992b6aSRichard Cissee    use ModuleListTrait;
4467992b6aSRichard Cissee
4506a438b4SGreg Roach    protected const ROUTE_URL = '/tree/{tree}/source-list';
4606a438b4SGreg Roach
47e72c24d6SGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
4833c746f1SGreg Roach    protected int $access_level = Auth::PRIV_USER;
49e72c24d6SGreg Roach
5067992b6aSRichard Cissee    /**
5106a438b4SGreg Roach     * Initialization.
5206a438b4SGreg Roach     *
5306a438b4SGreg Roach     * @return void
5406a438b4SGreg Roach     */
5506a438b4SGreg Roach    public function boot(): void
5606a438b4SGreg Roach    {
57*158900c2SGreg Roach        Registry::routeFactory()->routeMap()
5806a438b4SGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
5906a438b4SGreg Roach    }
6006a438b4SGreg Roach
6106a438b4SGreg Roach    /**
620cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
6367992b6aSRichard Cissee     *
6467992b6aSRichard Cissee     * @return string
6567992b6aSRichard Cissee     */
6667992b6aSRichard Cissee    public function title(): string
6767992b6aSRichard Cissee    {
6867992b6aSRichard Cissee        /* I18N: Name of a module/list */
6967992b6aSRichard Cissee        return I18N::translate('Sources');
7067992b6aSRichard Cissee    }
7167992b6aSRichard Cissee
7267992b6aSRichard Cissee    /**
7367992b6aSRichard Cissee     * A sentence describing what this module does.
7467992b6aSRichard Cissee     *
7567992b6aSRichard Cissee     * @return string
7667992b6aSRichard Cissee     */
7767992b6aSRichard Cissee    public function description(): string
7867992b6aSRichard Cissee    {
79b5e8e56bSGreg Roach        /* I18N: Description of the “Sources” module */
8067992b6aSRichard Cissee        return I18N::translate('A list of sources.');
8167992b6aSRichard Cissee    }
8267992b6aSRichard Cissee
8367992b6aSRichard Cissee    /**
8467992b6aSRichard Cissee     * CSS class for the URL.
8567992b6aSRichard Cissee     *
8667992b6aSRichard Cissee     * @return string
8767992b6aSRichard Cissee     */
8867992b6aSRichard Cissee    public function listMenuClass(): string
8967992b6aSRichard Cissee    {
9067992b6aSRichard Cissee        return 'menu-list-sour';
9167992b6aSRichard Cissee    }
9267992b6aSRichard Cissee
934db4b4a9SGreg Roach    /**
9406a438b4SGreg Roach     * @param Tree                                      $tree
9576d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
964db4b4a9SGreg Roach     *
9706a438b4SGreg Roach     * @return string
984db4b4a9SGreg Roach     */
9906a438b4SGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
10067992b6aSRichard Cissee    {
10106a438b4SGreg Roach        $parameters['tree'] = $tree->name();
1025229eadeSGreg Roach
10306a438b4SGreg Roach        return route(static::class, $parameters);
10467992b6aSRichard Cissee    }
10567992b6aSRichard Cissee
1064db4b4a9SGreg Roach    /**
10724f2a3afSGreg Roach     * @return array<string>
1084db4b4a9SGreg Roach     */
10967992b6aSRichard Cissee    public function listUrlAttributes(): array
11067992b6aSRichard Cissee    {
11167992b6aSRichard Cissee        return [];
11267992b6aSRichard Cissee    }
11367992b6aSRichard Cissee
1144db4b4a9SGreg Roach    /**
1154db4b4a9SGreg Roach     * @param Tree $tree
1164db4b4a9SGreg Roach     *
1174db4b4a9SGreg Roach     * @return bool
1184db4b4a9SGreg Roach     */
11967992b6aSRichard Cissee    public function listIsEmpty(Tree $tree): bool
12067992b6aSRichard Cissee    {
12167992b6aSRichard Cissee        return !DB::table('sources')
12267992b6aSRichard Cissee            ->where('s_file', '=', $tree->id())
12367992b6aSRichard Cissee            ->exists();
12467992b6aSRichard Cissee    }
12506a438b4SGreg Roach
12606a438b4SGreg Roach    /**
12706a438b4SGreg Roach     * Handle URLs generated by older versions of webtrees
12806a438b4SGreg Roach     *
12906a438b4SGreg Roach     * @param ServerRequestInterface $request
13006a438b4SGreg Roach     *
13106a438b4SGreg Roach     * @return ResponseInterface
13206a438b4SGreg Roach     */
13306a438b4SGreg Roach    public function getListAction(ServerRequestInterface $request): ResponseInterface
13406a438b4SGreg Roach    {
135b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
136b55cbc6bSGreg Roach
137b55cbc6bSGreg Roach        return redirect($this->listUrl($tree, $request->getQueryParams()));
13806a438b4SGreg Roach    }
13906a438b4SGreg Roach
14006a438b4SGreg Roach    /**
14106a438b4SGreg Roach     * @param ServerRequestInterface $request
14206a438b4SGreg Roach     *
14306a438b4SGreg Roach     * @return ResponseInterface
14406a438b4SGreg Roach     */
14506a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
14606a438b4SGreg Roach    {
147b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
148b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
14906a438b4SGreg Roach
15006a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
15106a438b4SGreg Roach
15206a438b4SGreg Roach        $sources = DB::table('sources')
15306a438b4SGreg Roach            ->where('s_file', '=', $tree->id())
15406a438b4SGreg Roach            ->get()
1556b9cb339SGreg Roach            ->map(Registry::sourceFactory()->mapper($tree))
15606a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter());
15706a438b4SGreg Roach
15806a438b4SGreg Roach        return $this->viewResponse('modules/source-list/page', [
15906a438b4SGreg Roach            'sources'   => $sources,
16006a438b4SGreg Roach            'title'     => I18N::translate('Sources'),
16106a438b4SGreg Roach            'tree'      => $tree,
16206a438b4SGreg Roach        ]);
16306a438b4SGreg Roach    }
16467992b6aSRichard Cissee}
165