1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 Aura\Router\RouterContainer; 23use Fig\Http\Message\RequestMethodInterface; 24use Fisharebest\Webtrees\Auth; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Menu; 28use Fisharebest\Webtrees\Registry; 29use Fisharebest\Webtrees\Validator; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function app; 35use function assert; 36use function route; 37 38/** 39 * Class FamilyBookChartModule 40 */ 41class FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 42{ 43 use ModuleChartTrait; 44 45 protected const ROUTE_URL = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}'; 46 47 // Defaults 48 public const DEFAULT_GENERATIONS = '2'; 49 public const DEFAULT_DESCENDANT_GENERATIONS = '5'; 50 protected const DEFAULT_PARAMETERS = [ 51 'book_size' => self::DEFAULT_GENERATIONS, 52 'generations' => self::DEFAULT_DESCENDANT_GENERATIONS, 53 'spouses' => false, 54 ]; 55 56 // Limits 57 protected const MINIMUM_BOOK_SIZE = 2; 58 protected const MAXIMUM_BOOK_SIZE = 5; 59 60 protected const MINIMUM_GENERATIONS = 2; 61 protected const MAXIMUM_GENERATIONS = 10; 62 63 /** 64 * Initialization. 65 * 66 * @return void 67 */ 68 public function boot(): void 69 { 70 $router_container = app(RouterContainer::class); 71 assert($router_container instanceof RouterContainer); 72 73 $router_container->getMap() 74 ->get(static::class, static::ROUTE_URL, $this) 75 ->allows(RequestMethodInterface::METHOD_POST) 76 ->tokens([ 77 'book_size' => '\d+', 78 'generations' => '\d+', 79 'spouses' => '1?', 80 ]); 81 } 82 83 /** 84 * How should this module be identified in the control panel, etc.? 85 * 86 * @return string 87 */ 88 public function title(): string 89 { 90 /* I18N: Name of a module/chart */ 91 return I18N::translate('Family book'); 92 } 93 94 /** 95 * A sentence describing what this module does. 96 * 97 * @return string 98 */ 99 public function description(): string 100 { 101 /* I18N: Description of the “FamilyBookChart” module */ 102 return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.'); 103 } 104 105 /** 106 * CSS class for the URL. 107 * 108 * @return string 109 */ 110 public function chartMenuClass(): string 111 { 112 return 'menu-chart-familybook'; 113 } 114 115 /** 116 * Return a menu item for this chart - for use in individual boxes. 117 * 118 * @param Individual $individual 119 * 120 * @return Menu|null 121 */ 122 public function chartBoxMenu(Individual $individual): ?Menu 123 { 124 return $this->chartMenu($individual); 125 } 126 127 /** 128 * The title for a specific instance of this chart. 129 * 130 * @param Individual $individual 131 * 132 * @return string 133 */ 134 public function chartTitle(Individual $individual): string 135 { 136 /* I18N: %s is an individual’s name */ 137 return I18N::translate('Family book of %s', $individual->fullName()); 138 } 139 140 /** 141 * The URL for a page showing chart options. 142 * 143 * @param Individual $individual 144 * @param array<bool|int|string|array<string>|null> $parameters 145 * 146 * @return string 147 */ 148 public function chartUrl(Individual $individual, array $parameters = []): string 149 { 150 return route(static::class, [ 151 'xref' => $individual->xref(), 152 'tree' => $individual->tree()->name(), 153 ] + $parameters + self::DEFAULT_PARAMETERS); 154 } 155 156 /** 157 * @param ServerRequestInterface $request 158 * 159 * @return ResponseInterface 160 */ 161 public function handle(ServerRequestInterface $request): ResponseInterface 162 { 163 $tree = Validator::attributes($request)->tree(); 164 $user = Validator::attributes($request)->user(); 165 $xref = Validator::attributes($request)->isXref()->string('xref'); 166 $book_size = Validator::attributes($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'); 167 $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 168 $spouses = Validator::attributes($request)->boolean('spouses'); 169 $ajax = Validator::queryParams($request)->boolean('ajax', false); 170 171 // Convert POST requests into GET requests for pretty URLs. 172 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 173 return redirect(route(static::class, [ 174 'tree' => $tree->name(), 175 'xref' => Validator::parsedBody($request)->string('xref', ''), 176 'book_size' => Validator::parsedBody($request)->string('book_size', ''), 177 'generations' => Validator::parsedBody($request)->string('generations', ''), 178 'spouses' => Validator::parsedBody($request)->string('spouses', ''), 179 ])); 180 } 181 182 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 183 184 $individual = Registry::individualFactory()->make($xref, $tree); 185 $individual = Auth::checkIndividualAccess($individual, false, true); 186 187 if ($ajax) { 188 $this->layout = 'layouts/ajax'; 189 190 return $this->viewResponse('modules/family-book-chart/chart', [ 191 'individual' => $individual, 192 'generations' => $generations, 193 'book_size' => $book_size, 194 'spouses' => $spouses, 195 ]); 196 } 197 198 $ajax_url = $this->chartUrl($individual, [ 199 'ajax' => true, 200 'book_size' => $book_size, 201 'generations' => $generations, 202 'spouses' => $spouses, 203 ]); 204 205 return $this->viewResponse('modules/family-book-chart/page', [ 206 'ajax_url' => $ajax_url, 207 'book_size' => $book_size, 208 'generations' => $generations, 209 'individual' => $individual, 210 'maximum_book_size' => self::MAXIMUM_BOOK_SIZE, 211 'minimum_book_size' => self::MINIMUM_BOOK_SIZE, 212 'maximum_generations' => self::MAXIMUM_GENERATIONS, 213 'minimum_generations' => self::MINIMUM_GENERATIONS, 214 'module' => $this->name(), 215 'spouses' => $spouses, 216 'title' => $this->chartTitle($individual), 217 'tree' => $tree, 218 ]); 219 } 220} 221