xref: /webtrees/app/Module/SourceListModule.php (revision 09482a558a7989d76059e7f9911605cf836b77ba)
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;
23*09482a55SGreg Roachuse Fisharebest\Webtrees\Auth;
2406a438b4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2506a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
27*09482a55SGreg Roachuse Fisharebest\Webtrees\Registry;
2867992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
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. */
48e72c24d6SGreg Roach    protected $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    {
5706a438b4SGreg Roach        $router_container = app(RouterContainer::class);
5806a438b4SGreg Roach        assert($router_container instanceof RouterContainer);
5906a438b4SGreg Roach
6006a438b4SGreg Roach        $router_container->getMap()
6106a438b4SGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
6206a438b4SGreg Roach    }
6306a438b4SGreg Roach
6406a438b4SGreg Roach    /**
650cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
6667992b6aSRichard Cissee     *
6767992b6aSRichard Cissee     * @return string
6867992b6aSRichard Cissee     */
6967992b6aSRichard Cissee    public function title(): string
7067992b6aSRichard Cissee    {
7167992b6aSRichard Cissee        /* I18N: Name of a module/list */
7267992b6aSRichard Cissee        return I18N::translate('Sources');
7367992b6aSRichard Cissee    }
7467992b6aSRichard Cissee
7567992b6aSRichard Cissee    /**
7667992b6aSRichard Cissee     * A sentence describing what this module does.
7767992b6aSRichard Cissee     *
7867992b6aSRichard Cissee     * @return string
7967992b6aSRichard Cissee     */
8067992b6aSRichard Cissee    public function description(): string
8167992b6aSRichard Cissee    {
82b5e8e56bSGreg Roach        /* I18N: Description of the “Sources” module */
8367992b6aSRichard Cissee        return I18N::translate('A list of sources.');
8467992b6aSRichard Cissee    }
8567992b6aSRichard Cissee
8667992b6aSRichard Cissee    /**
8767992b6aSRichard Cissee     * CSS class for the URL.
8867992b6aSRichard Cissee     *
8967992b6aSRichard Cissee     * @return string
9067992b6aSRichard Cissee     */
9167992b6aSRichard Cissee    public function listMenuClass(): string
9267992b6aSRichard Cissee    {
9367992b6aSRichard Cissee        return 'menu-list-sour';
9467992b6aSRichard Cissee    }
9567992b6aSRichard Cissee
964db4b4a9SGreg Roach    /**
9706a438b4SGreg Roach     * @param Tree                              $tree
98*09482a55SGreg Roach     * @param array<bool|int|string|array|null> $parameters
994db4b4a9SGreg Roach     *
10006a438b4SGreg Roach     * @return string
1014db4b4a9SGreg Roach     */
10206a438b4SGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
10367992b6aSRichard Cissee    {
10406a438b4SGreg Roach        $parameters['tree'] = $tree->name();
1055229eadeSGreg Roach
10606a438b4SGreg Roach        return route(static::class, $parameters);
10767992b6aSRichard Cissee    }
10867992b6aSRichard Cissee
1094db4b4a9SGreg Roach    /**
11024f2a3afSGreg Roach     * @return array<string>
1114db4b4a9SGreg Roach     */
11267992b6aSRichard Cissee    public function listUrlAttributes(): array
11367992b6aSRichard Cissee    {
11467992b6aSRichard Cissee        return [];
11567992b6aSRichard Cissee    }
11667992b6aSRichard Cissee
1174db4b4a9SGreg Roach    /**
1184db4b4a9SGreg Roach     * @param Tree $tree
1194db4b4a9SGreg Roach     *
1204db4b4a9SGreg Roach     * @return bool
1214db4b4a9SGreg Roach     */
12267992b6aSRichard Cissee    public function listIsEmpty(Tree $tree): bool
12367992b6aSRichard Cissee    {
12467992b6aSRichard Cissee        return !DB::table('sources')
12567992b6aSRichard Cissee            ->where('s_file', '=', $tree->id())
12667992b6aSRichard Cissee            ->exists();
12767992b6aSRichard Cissee    }
12806a438b4SGreg Roach
12906a438b4SGreg Roach    /**
13006a438b4SGreg Roach     * Handle URLs generated by older versions of webtrees
13106a438b4SGreg Roach     *
13206a438b4SGreg Roach     * @param ServerRequestInterface $request
13306a438b4SGreg Roach     *
13406a438b4SGreg Roach     * @return ResponseInterface
13506a438b4SGreg Roach     */
13606a438b4SGreg Roach    public function getListAction(ServerRequestInterface $request): ResponseInterface
13706a438b4SGreg Roach    {
13806a438b4SGreg Roach        return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams()));
13906a438b4SGreg Roach    }
14006a438b4SGreg Roach
14106a438b4SGreg Roach    /**
14206a438b4SGreg Roach     * @param ServerRequestInterface $request
14306a438b4SGreg Roach     *
14406a438b4SGreg Roach     * @return ResponseInterface
14506a438b4SGreg Roach     */
14606a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
14706a438b4SGreg Roach    {
14806a438b4SGreg Roach        $tree = $request->getAttribute('tree');
14906a438b4SGreg Roach        assert($tree instanceof Tree);
15006a438b4SGreg Roach
15106a438b4SGreg Roach        $user = $request->getAttribute('user');
15206a438b4SGreg Roach        assert($user instanceof UserInterface);
15306a438b4SGreg Roach
15406a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
15506a438b4SGreg Roach
15606a438b4SGreg Roach        $sources = DB::table('sources')
15706a438b4SGreg Roach            ->where('s_file', '=', $tree->id())
15806a438b4SGreg Roach            ->get()
1596b9cb339SGreg Roach            ->map(Registry::sourceFactory()->mapper($tree))
16006a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter());
16106a438b4SGreg Roach
16206a438b4SGreg Roach        return $this->viewResponse('modules/source-list/page', [
16306a438b4SGreg Roach            'sources'   => $sources,
16406a438b4SGreg Roach            'title'     => I18N::translate('Sources'),
16506a438b4SGreg Roach            'tree'      => $tree,
16606a438b4SGreg Roach        ]);
16706a438b4SGreg Roach    }
16867992b6aSRichard Cissee}
169