xref: /webtrees/app/Module/SourceListModule.php (revision 00b1984e374faddab4698e73088f0c964e529b65)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Http\Controllers\ListController;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\IndividualListService;
25use Fisharebest\Webtrees\Services\LocalizationService;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\Auth;
28use Illuminate\Database\Capsule\Manager as DB;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31
32use function assert;
33
34/**
35 * Class RepositoryListModule
36 */
37class SourceListModule extends AbstractModule implements ModuleListInterface
38{
39    use ModuleListTrait;
40
41    /** @var int The default access level for this module.  It can be changed in the control panel. */
42    protected $access_level = Auth::PRIV_USER;
43
44    /**
45     * How should this module be identified in the control panel, etc.?
46     *
47     * @return string
48     */
49    public function title(): string
50    {
51        /* I18N: Name of a module/list */
52        return I18N::translate('Sources');
53    }
54
55    /**
56     * A sentence describing what this module does.
57     *
58     * @return string
59     */
60    public function description(): string
61    {
62        /* I18N: Description of the “Sources” module */
63        return I18N::translate('A list of sources.');
64    }
65
66    /**
67     * CSS class for the URL.
68     *
69     * @return string
70     */
71    public function listMenuClass(): string
72    {
73        return 'menu-list-sour';
74    }
75
76    /**
77     * @param ServerRequestInterface $request
78     *
79     * @return ResponseInterface
80     */
81    public function getListAction(ServerRequestInterface $request): ResponseInterface
82    {
83        $tree = $request->getAttribute('tree');
84        assert($tree instanceof Tree);
85
86        $user = $request->getAttribute('user');
87
88        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
89
90        $listController = new ListController(app(IndividualListService::class), app(LocalizationService::class));
91        return $listController->sourceList($request);
92    }
93
94    /**
95     * @return string[]
96     */
97    public function listUrlAttributes(): array
98    {
99        return [];
100    }
101
102    /**
103     * @param Tree $tree
104     *
105     * @return bool
106     */
107    public function listIsEmpty(Tree $tree): bool
108    {
109        return !DB::table('sources')
110            ->where('s_file', '=', $tree->id())
111            ->exists();
112    }
113}
114