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 public function description(): string 84 { 85 /* I18N: Description of the “FamilyBookChart” module */ 86 return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.'); 87 } 88 89 /** 90 * CSS class for the URL. 91 * 92 * @return string 93 */ 94 public function chartMenuClass(): string 95 { 96 return 'menu-chart-familybook'; 97 } 98 99 /** 100 * Return a menu item for this chart - for use in individual boxes. 101 */ 102 public function chartBoxMenu(Individual $individual): Menu|null 103 { 104 return $this->chartMenu($individual); 105 } 106 107 /** 108 * The title for a specific instance of this chart. 109 * 110 * @param Individual $individual 111 * 112 * @return string 113 */ 114 public function chartTitle(Individual $individual): string 115 { 116 /* I18N: %s is an individual’s name */ 117 return I18N::translate('Family book of %s', $individual->fullName()); 118 } 119 120 /** 121 * The URL for a page showing chart options. 122 * 123 * @param Individual $individual 124 * @param array<bool|int|string|array<string>|null> $parameters 125 * 126 * @return string 127 */ 128 public function chartUrl(Individual $individual, array $parameters = []): string 129 { 130 return route(static::class, [ 131 'xref' => $individual->xref(), 132 'tree' => $individual->tree()->name(), 133 ] + $parameters + self::DEFAULT_PARAMETERS); 134 } 135 136 /** 137 * @param ServerRequestInterface $request 138 * 139 * @return ResponseInterface 140 */ 141 public function handle(ServerRequestInterface $request): ResponseInterface 142 { 143 $tree = Validator::attributes($request)->tree(); 144 $user = Validator::attributes($request)->user(); 145 $xref = Validator::attributes($request)->isXref()->string('xref'); 146 $book_size = Validator::attributes($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'); 147 $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 148 $spouses = Validator::attributes($request)->boolean('spouses', false); 149 $ajax = Validator::queryParams($request)->boolean('ajax', false); 150 151 // Convert POST requests into GET requests for pretty URLs. 152 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 153 return redirect(route(static::class, [ 154 'tree' => $tree->name(), 155 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 156 'book_size' => Validator::parsedBody($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'), 157 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 158 'spouses' => Validator::parsedBody($request)->boolean('spouses', false), 159 ])); 160 } 161 162 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 163 164 $individual = Registry::individualFactory()->make($xref, $tree); 165 $individual = Auth::checkIndividualAccess($individual, false, true); 166 167 if ($ajax) { 168 $this->layout = 'layouts/ajax'; 169 170 return $this->viewResponse('modules/family-book-chart/chart', [ 171 'individual' => $individual, 172 'generations' => $generations, 173 'book_size' => $book_size, 174 'spouses' => $spouses, 175 ]); 176 } 177 178 $ajax_url = $this->chartUrl($individual, [ 179 'ajax' => true, 180 'book_size' => $book_size, 181 'generations' => $generations, 182 'spouses' => $spouses, 183 ]); 184 185 return $this->viewResponse('modules/family-book-chart/page', [ 186 'ajax_url' => $ajax_url, 187 'book_size' => $book_size, 188 'generations' => $generations, 189 'individual' => $individual, 190 'maximum_book_size' => self::MAXIMUM_BOOK_SIZE, 191 'minimum_book_size' => self::MINIMUM_BOOK_SIZE, 192 'maximum_generations' => self::MAXIMUM_GENERATIONS, 193 'minimum_generations' => self::MINIMUM_GENERATIONS, 194 'module' => $this->name(), 195 'spouses' => $spouses, 196 'title' => $this->chartTitle($individual), 197 'tree' => $tree, 198 ]); 199 } 200} 201