xref: /webtrees/app/Module/RepositoryListModule.php (revision 62fa6fd79d2f943d0a6f3e88ffeab601aec582a6)
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\Repository;
25use Fisharebest\Webtrees\Services\IndividualListService;
26use Fisharebest\Webtrees\Services\LocalizationService;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\Auth;
29use Illuminate\Database\Capsule\Manager as DB;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32
33use function assert;
34
35/**
36 * Class RepositoryListModule
37 */
38class RepositoryListModule extends AbstractModule implements ModuleListInterface
39{
40    use ModuleListTrait;
41
42    /** @var int The default access level for this module.  It can be changed in the control panel. */
43    protected $access_level = Auth::PRIV_USER;
44
45    /**
46     * How should this module be identified in the control panel, etc.?
47     *
48     * @return string
49     */
50    public function title(): string
51    {
52        /* I18N: Name of a module/list */
53        return I18N::translate('Repositories');
54    }
55
56    /**
57     * A sentence describing what this module does.
58     *
59     * @return string
60     */
61    public function description(): string
62    {
63        /* I18N: Description of the “Repositories” module */
64        return I18N::translate('A list of repositories.');
65    }
66
67    /**
68     * CSS class for the URL.
69     *
70     * @return string
71     */
72    public function listMenuClass(): string
73    {
74        return 'menu-list-repo';
75    }
76
77    /**
78     * @param ServerRequestInterface $request
79     *
80     * @return ResponseInterface
81     */
82    public function getListAction(ServerRequestInterface $request): ResponseInterface
83    {
84        $tree = $request->getAttribute('tree');
85        assert($tree instanceof Tree);
86
87        $user = $request->getAttribute('user');
88
89        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
90
91        $listController = new ListController(app(IndividualListService::class), app(LocalizationService::class));
92        return $listController->repositoryList($request);
93    }
94
95    /**
96     * @return string[]
97     */
98    public function listUrlAttributes(): array
99    {
100        return [];
101    }
102
103    /**
104     * @param Tree $tree
105     *
106     * @return bool
107     */
108    public function listIsEmpty(Tree $tree): bool
109    {
110        return !DB::table('other')
111            ->where('o_file', '=', $tree->id())
112            ->where('o_type', '=', Repository::RECORD_TYPE)
113            ->exists();
114    }
115}
116