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