xref: /webtrees/app/Module/NoteListModule.php (revision 76d39c55735cfa9ad0972b0dd530e96b051f9ebe)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
2406a438b4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2606a438b4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2767992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
28e72c24d6SGreg Roachuse Fisharebest\Webtrees\Note;
2967992b6aSRichard Cisseeuse Fisharebest\Webtrees\Tree;
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    {
5506a438b4SGreg Roach        $router_container = app(RouterContainer::class);
5606a438b4SGreg Roach        assert($router_container instanceof RouterContainer);
5706a438b4SGreg Roach
5806a438b4SGreg Roach        $router_container->getMap()
5906a438b4SGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
6006a438b4SGreg Roach    }
6106a438b4SGreg Roach
6267992b6aSRichard Cissee    /**
630cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
6467992b6aSRichard Cissee     *
6567992b6aSRichard Cissee     * @return string
6667992b6aSRichard Cissee     */
6767992b6aSRichard Cissee    public function title(): string
6867992b6aSRichard Cissee    {
6967992b6aSRichard Cissee        /* I18N: Name of a module/list */
7067992b6aSRichard Cissee        return I18N::translate('Shared notes');
7167992b6aSRichard Cissee    }
7267992b6aSRichard Cissee
7367992b6aSRichard Cissee    /**
7467992b6aSRichard Cissee     * A sentence describing what this module does.
7567992b6aSRichard Cissee     *
7667992b6aSRichard Cissee     * @return string
7767992b6aSRichard Cissee     */
7867992b6aSRichard Cissee    public function description(): string
7967992b6aSRichard Cissee    {
80b5e8e56bSGreg Roach        /* I18N: Description of the “Shared notes” module */
8167992b6aSRichard Cissee        return I18N::translate('A list of shared notes.');
8267992b6aSRichard Cissee    }
8367992b6aSRichard Cissee
8467992b6aSRichard Cissee    /**
8567992b6aSRichard Cissee     * CSS class for the URL.
8667992b6aSRichard Cissee     *
8767992b6aSRichard Cissee     * @return string
8867992b6aSRichard Cissee     */
8967992b6aSRichard Cissee    public function listMenuClass(): string
9067992b6aSRichard Cissee    {
9167992b6aSRichard Cissee        return 'menu-list-note';
9267992b6aSRichard Cissee    }
9367992b6aSRichard Cissee
944db4b4a9SGreg Roach    /**
9506a438b4SGreg Roach     * @param Tree                                      $tree
96*76d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
974db4b4a9SGreg Roach     *
9806a438b4SGreg Roach     * @return string
994db4b4a9SGreg Roach     */
10006a438b4SGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
10167992b6aSRichard Cissee    {
10206a438b4SGreg Roach        $parameters['tree'] = $tree->name();
1035229eadeSGreg Roach
10406a438b4SGreg Roach        return route(static::class, $parameters);
10567992b6aSRichard Cissee    }
10667992b6aSRichard Cissee
1074db4b4a9SGreg Roach    /**
10824f2a3afSGreg Roach     * @return array<string>
1094db4b4a9SGreg Roach     */
11067992b6aSRichard Cissee    public function listUrlAttributes(): array
11167992b6aSRichard Cissee    {
11267992b6aSRichard Cissee        return [];
11367992b6aSRichard Cissee    }
11467992b6aSRichard Cissee
1154db4b4a9SGreg Roach    /**
1164db4b4a9SGreg Roach     * @param Tree $tree
1174db4b4a9SGreg Roach     *
1184db4b4a9SGreg Roach     * @return bool
1194db4b4a9SGreg Roach     */
12067992b6aSRichard Cissee    public function listIsEmpty(Tree $tree): bool
12167992b6aSRichard Cissee    {
12267992b6aSRichard Cissee        return !DB::table('other')
12367992b6aSRichard Cissee            ->where('o_file', '=', $tree->id())
124e72c24d6SGreg Roach            ->where('o_type', '=', Note::RECORD_TYPE)
12567992b6aSRichard Cissee            ->exists();
12667992b6aSRichard Cissee    }
12706a438b4SGreg Roach
12806a438b4SGreg Roach    /**
12906a438b4SGreg Roach     * Handle URLs generated by older versions of webtrees
13006a438b4SGreg Roach     *
13106a438b4SGreg Roach     * @param ServerRequestInterface $request
13206a438b4SGreg Roach     *
13306a438b4SGreg Roach     * @return ResponseInterface
13406a438b4SGreg Roach     */
13506a438b4SGreg Roach    public function getListAction(ServerRequestInterface $request): ResponseInterface
13606a438b4SGreg Roach    {
13706a438b4SGreg Roach        return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams()));
13806a438b4SGreg Roach    }
13906a438b4SGreg Roach
14006a438b4SGreg Roach    /**
14106a438b4SGreg Roach     * @param ServerRequestInterface $request
14206a438b4SGreg Roach     *
14306a438b4SGreg Roach     * @return ResponseInterface
14406a438b4SGreg Roach     */
14506a438b4SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
14606a438b4SGreg Roach    {
14706a438b4SGreg Roach        $tree = $request->getAttribute('tree');
14806a438b4SGreg Roach        assert($tree instanceof Tree);
14906a438b4SGreg Roach
15006a438b4SGreg Roach        $user = $request->getAttribute('user');
15106a438b4SGreg Roach        assert($user instanceof UserInterface);
15206a438b4SGreg Roach
15306a438b4SGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
15406a438b4SGreg Roach
15506a438b4SGreg Roach        $notes = DB::table('other')
15606a438b4SGreg Roach            ->where('o_file', '=', $tree->id())
15706a438b4SGreg Roach            ->where('o_type', '=', Note::RECORD_TYPE)
15806a438b4SGreg Roach            ->get()
1596b9cb339SGreg Roach            ->map(Registry::noteFactory()->mapper($tree))
16006a438b4SGreg Roach            ->filter(GedcomRecord::accessFilter());
16106a438b4SGreg Roach
16206a438b4SGreg Roach        return $this->viewResponse('modules/note-list/page', [
16306a438b4SGreg Roach            'notes' => $notes,
16406a438b4SGreg Roach            'title' => I18N::translate('Notes'),
16506a438b4SGreg Roach            'tree'  => $tree,
16606a438b4SGreg Roach        ]);
16706a438b4SGreg Roach    }
16867992b6aSRichard Cissee}
169