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