xref: /webtrees/app/Module/NoteListModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
667992b6aSRichard Cissee * This program is free software: you can redistribute it and/or modify
767992b6aSRichard Cissee * it under the terms of the GNU General Public License as published by
867992b6aSRichard Cissee * the Free Software Foundation, either version 3 of the License, or
967992b6aSRichard Cissee * (at your option) any later version.
1067992b6aSRichard Cissee * This program is distributed in the hope that it will be useful,
1167992b6aSRichard Cissee * but WITHOUT ANY WARRANTY; without even the implied warranty of
1267992b6aSRichard Cissee * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1367992b6aSRichard Cissee * GNU General Public License for more details.
1467992b6aSRichard Cissee * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1667992b6aSRichard Cissee */
17fcfa147eSGreg Roach
1867992b6aSRichard Cisseedeclare(strict_types=1);
1967992b6aSRichard Cissee
2067992b6aSRichard Cisseenamespace Fisharebest\Webtrees\Module;
2167992b6aSRichard Cissee
2206a438b4SGreg Roachuse Aura\Router\RouterContainer;
2306a438b4SGreg Roachuse Fisharebest\Webtrees\Auth;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2506a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2667992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
27e72c24d6SGreg Roachuse Fisharebest\Webtrees\Note;
2867992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
3067992b6aSRichard Cisseeuse Illuminate\Database\Capsule\Manager as DB;
316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
3257ab2231SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3306a438b4SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
34f3874e19SGreg Roach
3506a438b4SGreg Roachuse function app;
365229eadeSGreg Roachuse function assert;
3706a438b4SGreg Roachuse function redirect;
3867992b6aSRichard Cissee
3967992b6aSRichard Cissee/**
40e72c24d6SGreg Roach * Class NoteListModule
4167992b6aSRichard Cissee */
4206a438b4SGreg Roachclass NoteListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
4367992b6aSRichard Cissee{
4467992b6aSRichard Cissee    use ModuleListTrait;
4567992b6aSRichard Cissee
4606a438b4SGreg Roach    protected const ROUTE_URL = '/tree/{tree}/note-list';
4706a438b4SGreg Roach
4806a438b4SGreg Roach    /**
4906a438b4SGreg Roach     * Initialization.
5006a438b4SGreg Roach     *
5106a438b4SGreg Roach     * @return void
5206a438b4SGreg Roach     */
5306a438b4SGreg Roach    public function boot(): void
5406a438b4SGreg Roach    {
55158900c2SGreg Roach        Registry::routeFactory()->routeMap()
5606a438b4SGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
5706a438b4SGreg Roach    }
5806a438b4SGreg Roach
5967992b6aSRichard Cissee    /**
600cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
6167992b6aSRichard Cissee     *
6267992b6aSRichard Cissee     * @return string
6367992b6aSRichard Cissee     */
6467992b6aSRichard Cissee    public function title(): string
6567992b6aSRichard Cissee    {
6667992b6aSRichard Cissee        /* I18N: Name of a module/list */
6767992b6aSRichard Cissee        return I18N::translate('Shared notes');
6867992b6aSRichard Cissee    }
6967992b6aSRichard Cissee
7067992b6aSRichard Cissee    /**
7167992b6aSRichard Cissee     * A sentence describing what this module does.
7267992b6aSRichard Cissee     *
7367992b6aSRichard Cissee     * @return string
7467992b6aSRichard Cissee     */
7567992b6aSRichard Cissee    public function description(): string
7667992b6aSRichard Cissee    {
77b5e8e56bSGreg Roach        /* I18N: Description of the “Shared notes” module */
7867992b6aSRichard Cissee        return I18N::translate('A list of shared notes.');
7967992b6aSRichard Cissee    }
8067992b6aSRichard Cissee
8167992b6aSRichard Cissee    /**
8267992b6aSRichard Cissee     * CSS class for the URL.
8367992b6aSRichard Cissee     *
8467992b6aSRichard Cissee     * @return string
8567992b6aSRichard Cissee     */
8667992b6aSRichard Cissee    public function listMenuClass(): string
8767992b6aSRichard Cissee    {
8867992b6aSRichard Cissee        return 'menu-list-note';
8967992b6aSRichard Cissee    }
9067992b6aSRichard Cissee
914db4b4a9SGreg Roach    /**
9206a438b4SGreg Roach     * @param Tree                                      $tree
9376d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
944db4b4a9SGreg Roach     *
9506a438b4SGreg Roach     * @return string
964db4b4a9SGreg Roach     */
9706a438b4SGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
9867992b6aSRichard Cissee    {
9906a438b4SGreg Roach        $parameters['tree'] = $tree->name();
1005229eadeSGreg Roach
10106a438b4SGreg Roach        return route(static::class, $parameters);
10267992b6aSRichard Cissee    }
10367992b6aSRichard Cissee
1044db4b4a9SGreg Roach    /**
10524f2a3afSGreg Roach     * @return array<string>
1064db4b4a9SGreg Roach     */
10767992b6aSRichard Cissee    public function listUrlAttributes(): array
10867992b6aSRichard Cissee    {
10967992b6aSRichard Cissee        return [];
11067992b6aSRichard Cissee    }
11167992b6aSRichard Cissee
1124db4b4a9SGreg Roach    /**
1134db4b4a9SGreg Roach     * @param Tree $tree
1144db4b4a9SGreg Roach     *
1154db4b4a9SGreg Roach     * @return bool
1164db4b4a9SGreg Roach     */
11767992b6aSRichard Cissee    public function listIsEmpty(Tree $tree): bool
11867992b6aSRichard Cissee    {
11967992b6aSRichard Cissee        return !DB::table('other')
12067992b6aSRichard Cissee            ->where('o_file', '=', $tree->id())
121e72c24d6SGreg Roach            ->where('o_type', '=', Note::RECORD_TYPE)
12267992b6aSRichard Cissee            ->exists();
12367992b6aSRichard Cissee    }
12406a438b4SGreg Roach
12506a438b4SGreg Roach    /**
12606a438b4SGreg Roach     * Handle URLs generated by older versions of webtrees
12706a438b4SGreg Roach     *
12806a438b4SGreg Roach     * @param ServerRequestInterface $request
12906a438b4SGreg Roach     *
13006a438b4SGreg Roach     * @return ResponseInterface
13106a438b4SGreg Roach     */
13206a438b4SGreg Roach    public function getListAction(ServerRequestInterface $request): ResponseInterface
13306a438b4SGreg Roach    {
134b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
135b55cbc6bSGreg Roach
136b55cbc6bSGreg Roach        return redirect($this->listUrl($tree, $request->getQueryParams()));
13706a438b4SGreg Roach    }
13806a438b4SGreg Roach
13906a438b4SGreg Roach    /**
14006a438b4SGreg Roach     * @param ServerRequestInterface $request
14106a438b4SGreg Roach     *
14206a438b4SGreg Roach     * @return ResponseInterface
14306a438b4SGreg Roach     */
14406a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
14506a438b4SGreg Roach    {
146b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
147b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
14806a438b4SGreg Roach
14906a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
15006a438b4SGreg Roach
15106a438b4SGreg Roach        $notes = DB::table('other')
15206a438b4SGreg Roach            ->where('o_file', '=', $tree->id())
15306a438b4SGreg Roach            ->where('o_type', '=', Note::RECORD_TYPE)
15406a438b4SGreg Roach            ->get()
1556b9cb339SGreg Roach            ->map(Registry::noteFactory()->mapper($tree))
15606a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter());
15706a438b4SGreg Roach
15806a438b4SGreg Roach        return $this->viewResponse('modules/note-list/page', [
15906a438b4SGreg Roach            'notes' => $notes,
16006a438b4SGreg Roach            'title' => I18N::translate('Notes'),
16106a438b4SGreg Roach            'tree'  => $tree,
16206a438b4SGreg Roach        ]);
16306a438b4SGreg Roach    }
16467992b6aSRichard Cissee}
165