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