xref: /webtrees/app/Module/NoteListModule.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Note;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\Validator;
29use Illuminate\Database\Capsule\Manager as DB;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function redirect;
35
36/**
37 * Class NoteListModule
38 */
39class 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