xref: /webtrees/app/Module/NoteListModule.php (revision f0c88a961929e1d14256ee4286994384bb29fbc1)
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     * Handle URLs generated by older versions of webtrees
124     *
125     * @param ServerRequestInterface $request
126     *
127     * @return ResponseInterface
128     */
129    public function getListAction(ServerRequestInterface $request): ResponseInterface
130    {
131        $tree = Validator::attributes($request)->tree();
132
133        return redirect($this->listUrl($tree, $request->getQueryParams()));
134    }
135
136    /**
137     * @param ServerRequestInterface $request
138     *
139     * @return ResponseInterface
140     */
141    public function handle(ServerRequestInterface $request): ResponseInterface
142    {
143        $tree = Validator::attributes($request)->tree();
144        $user = Validator::attributes($request)->user();
145
146        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
147
148        $notes = DB::table('other')
149            ->where('o_file', '=', $tree->id())
150            ->where('o_type', '=', Note::RECORD_TYPE)
151            ->get()
152            ->map(Registry::noteFactory()->mapper($tree))
153            ->filter(GedcomRecord::accessFilter());
154
155        return $this->viewResponse('modules/note-list/page', [
156            'notes' => $notes,
157            'title' => I18N::translate('Notes'),
158            'tree'  => $tree,
159        ]);
160    }
161}
162