xref: /webtrees/app/Module/NoteListModule.php (revision 9d74ed0ab3ae61aa8ae07578e026f14020844ce9)
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    public function description(): string
66    {
67        /* I18N: Description of the “Shared notes” module */
68        return I18N::translate('A list of shared notes.');
69    }
70
71    /**
72     * CSS class for the URL.
73     *
74     * @return string
75     */
76    public function listMenuClass(): string
77    {
78        return 'menu-list-note';
79    }
80
81    /**
82     * @param Tree                                      $tree
83     * @param array<bool|int|string|array<string>|null> $parameters
84     *
85     * @return string
86     */
87    public function listUrl(Tree $tree, array $parameters = []): string
88    {
89        $parameters['tree'] = $tree->name();
90
91        return route(static::class, $parameters);
92    }
93
94    /**
95     * @return array<string>
96     */
97    public function listUrlAttributes(): array
98    {
99        return [];
100    }
101
102    /**
103     * @param Tree $tree
104     *
105     * @return bool
106     */
107    public function listIsEmpty(Tree $tree): bool
108    {
109        return !DB::table('other')
110            ->where('o_file', '=', $tree->id())
111            ->where('o_type', '=', Note::RECORD_TYPE)
112            ->exists();
113    }
114
115    /**
116     * @param ServerRequestInterface $request
117     *
118     * @return ResponseInterface
119     */
120    public function handle(ServerRequestInterface $request): ResponseInterface
121    {
122        $tree = Validator::attributes($request)->tree();
123        $user = Validator::attributes($request)->user();
124
125        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
126
127        $notes = DB::table('other')
128            ->where('o_file', '=', $tree->id())
129            ->where('o_type', '=', Note::RECORD_TYPE)
130            ->get()
131            ->map(Registry::noteFactory()->mapper($tree))
132            ->filter(GedcomRecord::accessFilter());
133
134        return $this->viewResponse('modules/note-list/page', [
135            'notes' => $notes,
136            'title' => I18N::translate('Notes'),
137            'tree'  => $tree,
138        ]);
139    }
140}
141