1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Aura\Router\RouterContainer; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Contracts\UserInterface; 25use Fisharebest\Webtrees\Factory; 26use Fisharebest\Webtrees\GedcomRecord; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Note; 29use Fisharebest\Webtrees\Tree; 30use Illuminate\Database\Capsule\Manager as DB; 31use Psr\Http\Message\ResponseInterface; 32use Psr\Http\Message\ServerRequestInterface; 33use Psr\Http\Server\RequestHandlerInterface; 34 35use function app; 36use function assert; 37use function redirect; 38 39/** 40 * Class NoteListModule 41 */ 42class NoteListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface 43{ 44 use ModuleListTrait; 45 46 protected const ROUTE_URL = '/tree/{tree}/note-list'; 47 48 /** 49 * Initialization. 50 * 51 * @return void 52 */ 53 public function boot(): void 54 { 55 $router_container = app(RouterContainer::class); 56 assert($router_container instanceof RouterContainer); 57 58 $router_container->getMap() 59 ->get(static::class, static::ROUTE_URL, $this); 60 } 61 62 /** 63 * How should this module be identified in the control panel, etc.? 64 * 65 * @return string 66 */ 67 public function title(): string 68 { 69 /* I18N: Name of a module/list */ 70 return I18N::translate('Shared notes'); 71 } 72 73 /** 74 * A sentence describing what this module does. 75 * 76 * @return string 77 */ 78 public function description(): string 79 { 80 /* I18N: Description of the “Shared notes” module */ 81 return I18N::translate('A list of shared notes.'); 82 } 83 84 /** 85 * CSS class for the URL. 86 * 87 * @return string 88 */ 89 public function listMenuClass(): string 90 { 91 return 'menu-list-note'; 92 } 93 94 /** 95 * @param Tree $tree 96 * @param mixed[] $parameters 97 * 98 * @return string 99 */ 100 public function listUrl(Tree $tree, array $parameters = []): string 101 { 102 $parameters['tree'] = $tree->name(); 103 104 return route(static::class, $parameters); 105 } 106 107 /** 108 * @return string[] 109 */ 110 public function listUrlAttributes(): array 111 { 112 return []; 113 } 114 115 /** 116 * @param Tree $tree 117 * 118 * @return bool 119 */ 120 public function listIsEmpty(Tree $tree): bool 121 { 122 return !DB::table('other') 123 ->where('o_file', '=', $tree->id()) 124 ->where('o_type', '=', Note::RECORD_TYPE) 125 ->exists(); 126 } 127 128 /** 129 * Handle URLs generated by older versions of webtrees 130 * 131 * @param ServerRequestInterface $request 132 * 133 * @return ResponseInterface 134 */ 135 public function getListAction(ServerRequestInterface $request): ResponseInterface 136 { 137 return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams())); 138 } 139 140 /** 141 * @param ServerRequestInterface $request 142 * 143 * @return ResponseInterface 144 */ 145 public function handle(ServerRequestInterface $request): ResponseInterface 146 { 147 $tree = $request->getAttribute('tree'); 148 assert($tree instanceof Tree); 149 150 $user = $request->getAttribute('user'); 151 assert($user instanceof UserInterface); 152 153 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 154 155 $notes = DB::table('other') 156 ->where('o_file', '=', $tree->id()) 157 ->where('o_type', '=', Note::RECORD_TYPE) 158 ->get() 159 ->map(Factory::note()->mapper($tree)) 160 ->filter(GedcomRecord::accessFilter()); 161 162 return $this->viewResponse('modules/note-list/page', [ 163 'notes' => $notes, 164 'title' => I18N::translate('Notes'), 165 'tree' => $tree, 166 ]); 167 } 168} 169