1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 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 Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Http\Controllers\BranchesController; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\ModuleService; 26use Fisharebest\Webtrees\Tree; 27use Psr\Http\Message\ResponseInterface; 28use Psr\Http\Message\ServerRequestInterface; 29 30use function assert; 31use function redirect; 32use function route; 33 34/** 35 * Class BranchesListModule 36 */ 37class BranchesListModule extends AbstractModule implements ModuleListInterface 38{ 39 use ModuleListTrait; 40 41 /** 42 * How should this module be identified in the control panel, etc.? 43 * 44 * @return string 45 */ 46 public function title(): string 47 { 48 /* I18N: Name of a module/list */ 49 return I18N::translate('Branches'); 50 } 51 52 /** 53 * A sentence describing what this module does. 54 * 55 * @return string 56 */ 57 public function description(): string 58 { 59 /* I18N: Description of the “Branches” module */ 60 return I18N::translate('A list of branches of a family.'); 61 } 62 63 /** 64 * CSS class for the URL. 65 * 66 * @return string 67 */ 68 public function listMenuClass(): string 69 { 70 return 'menu-branches'; 71 } 72 73 /** 74 * @param Tree $tree 75 * @param mixed[] $parameters 76 * 77 * @return string 78 */ 79 public function listUrl(Tree $tree, array $parameters = []): string 80 { 81 return route('module', [ 82 'module' => $this->name(), 83 'action' => 'Page', 84 'tree' => $tree->name(), 85 ] + $parameters); 86 } 87 88 /** 89 * @param ServerRequestInterface $request 90 * 91 * @return ResponseInterface 92 */ 93 public function getPageAction(ServerRequestInterface $request): ResponseInterface 94 { 95 $tree = $request->getAttribute('tree'); 96 assert($tree instanceof Tree); 97 98 $user = $request->getAttribute('user'); 99 100 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 101 102 $listController = new BranchesController(app(ModuleService::class)); 103 return $listController->page($request); 104 } 105 106 /** 107 * @param ServerRequestInterface $request 108 * 109 * @return ResponseInterface 110 */ 111 public function postPageAction(ServerRequestInterface $request): ResponseInterface 112 { 113 $tree = $request->getAttribute('tree'); 114 assert($tree instanceof Tree); 115 116 return redirect(route('module', [ 117 'module' => $this->name(), 118 'action' => 'Page', 119 'surname' => $request->getParsedBody()['surname'] ?? '', 120 'soundex_dm' => $request->getParsedBody()['soundex_dm'] ?? '', 121 'soundex_std' => $request->getParsedBody()['soundex_std'] ?? '', 122 'tree' => $tree->name(), 123 ])); 124 } 125 126 /** 127 * @param ServerRequestInterface $request 128 * 129 * @return ResponseInterface 130 */ 131 public function getListAction(ServerRequestInterface $request): ResponseInterface 132 { 133 $tree = $request->getAttribute('tree'); 134 assert($tree instanceof Tree); 135 136 $user = $request->getAttribute('user'); 137 138 Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); 139 140 return app(BranchesController::class)->list($request); 141 } 142 143 /** 144 * @return string[] 145 */ 146 public function listUrlAttributes(): array 147 { 148 return []; 149 } 150} 151