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

NoteListModule::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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