Passed
Push — main ( f783aa...748dbe )
by Greg
06:01
created

SourceListModule::getListAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 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 <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\GedcomRecord;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Registry;
26
use Fisharebest\Webtrees\Tree;
27
use Fisharebest\Webtrees\Validator;
28
use Illuminate\Database\Capsule\Manager as DB;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function redirect;
34
35
/**
36
 * Class RepositoryListModule
37
 */
38
class SourceListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
39
{
40
    use ModuleListTrait;
41
42
    protected const ROUTE_URL = '/tree/{tree}/source-list';
43
44
    /** @var int The default access level for this module.  It can be changed in the control panel. */
45
    protected int $access_level = Auth::PRIV_USER;
46
47
    /**
48
     * Initialization.
49
     *
50
     * @return void
51
     */
52
    public function boot(): void
53
    {
54
        Registry::routeFactory()->routeMap()
55
            ->get(static::class, static::ROUTE_URL, $this);
56
    }
57
58
    /**
59
     * How should this module be identified in the control panel, etc.?
60
     *
61
     * @return string
62
     */
63
    public function title(): string
64
    {
65
        /* I18N: Name of a module/list */
66
        return I18N::translate('Sources');
67
    }
68
69
    /**
70
     * A sentence describing what this module does.
71
     *
72
     * @return string
73
     */
74
    public function description(): string
75
    {
76
        /* I18N: Description of the “Sources” module */
77
        return I18N::translate('A list of sources.');
78
    }
79
80
    /**
81
     * CSS class for the URL.
82
     *
83
     * @return string
84
     */
85
    public function listMenuClass(): string
86
    {
87
        return 'menu-list-sour';
88
    }
89
90
    /**
91
     * @param Tree                                      $tree
92
     * @param array<bool|int|string|array<string>|null> $parameters
93
     *
94
     * @return string
95
     */
96
    public function listUrl(Tree $tree, array $parameters = []): string
97
    {
98
        $parameters['tree'] = $tree->name();
99
100
        return route(static::class, $parameters);
101
    }
102
103
    /**
104
     * @return array<string>
105
     */
106
    public function listUrlAttributes(): array
107
    {
108
        return [];
109
    }
110
111
    /**
112
     * @param Tree $tree
113
     *
114
     * @return bool
115
     */
116
    public function listIsEmpty(Tree $tree): bool
117
    {
118
        return !DB::table('sources')
119
            ->where('s_file', '=', $tree->id())
120
            ->exists();
121
    }
122
123
    /**
124
     * @param ServerRequestInterface $request
125
     *
126
     * @return ResponseInterface
127
     */
128
    public function handle(ServerRequestInterface $request): ResponseInterface
129
    {
130
        $tree = Validator::attributes($request)->tree();
131
        $user = Validator::attributes($request)->user();
132
133
        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
134
135
        $sources = DB::table('sources')
136
            ->where('s_file', '=', $tree->id())
137
            ->get()
138
            ->map(Registry::sourceFactory()->mapper($tree))
139
            ->filter(GedcomRecord::accessFilter());
140
141
        return $this->viewResponse('modules/source-list/page', [
142
            'sources'   => $sources,
143
            'title'     => I18N::translate('Sources'),
144
            'tree'      => $tree,
145
        ]);
146
    }
147
}
148