1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify 7168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by 8168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or 9168ff6f3Sric2016 * (at your option) any later version. 10168ff6f3Sric2016 * This program is distributed in the hope that it will be useful, 11168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13168ff6f3Sric2016 * GNU General Public License for more details. 14168ff6f3Sric2016 * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16168ff6f3Sric2016 */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 20168ff6f3Sric2016namespace Fisharebest\Webtrees\Module; 21168ff6f3Sric2016 2271378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 23389266c0SGreg Roachuse Fisharebest\Webtrees\Auth; 24168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 25168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 2709482a55SGreg Roachuse Fisharebest\Webtrees\Registry; 28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3171378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 323976b470SGreg Roach 3371378461SGreg Roachuse function route; 34168ff6f3Sric2016 35168ff6f3Sric2016/** 36168ff6f3Sric2016 * Class FamilyBookChartModule 37168ff6f3Sric2016 */ 3871378461SGreg Roachclass FamilyBookChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 39c1010edaSGreg Roach{ 4049a243cbSGreg Roach use ModuleChartTrait; 4149a243cbSGreg Roach 4272f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/family-book-{book_size}-{generations}-{spouses}/{xref}'; 4371378461SGreg Roach 44389266c0SGreg Roach // Defaults 4571378461SGreg Roach public const DEFAULT_GENERATIONS = '2'; 4671378461SGreg Roach public const DEFAULT_DESCENDANT_GENERATIONS = '5'; 4771378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 4871378461SGreg Roach 'book_size' => self::DEFAULT_GENERATIONS, 4971378461SGreg Roach 'generations' => self::DEFAULT_DESCENDANT_GENERATIONS, 5071378461SGreg Roach 'spouses' => false, 5171378461SGreg Roach ]; 52389266c0SGreg Roach 53e759aebbSGreg Roach // Limits 54b55cbc6bSGreg Roach protected const MINIMUM_BOOK_SIZE = 2; 55b55cbc6bSGreg Roach protected const MAXIMUM_BOOK_SIZE = 5; 56b55cbc6bSGreg Roach 5771378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 5871378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 5971378461SGreg Roach 6071378461SGreg Roach /** 6171378461SGreg Roach * Initialization. 6271378461SGreg Roach * 639e18e23bSGreg Roach * @return void 6471378461SGreg Roach */ 659e18e23bSGreg Roach public function boot(): void 6671378461SGreg Roach { 67158900c2SGreg Roach Registry::routeFactory()->routeMap() 6872f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 69158900c2SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 7071378461SGreg Roach } 71e759aebbSGreg Roach 72168ff6f3Sric2016 /** 730cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 74168ff6f3Sric2016 * 75168ff6f3Sric2016 * @return string 76168ff6f3Sric2016 */ 7749a243cbSGreg Roach public function title(): string 78c1010edaSGreg Roach { 79bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 80bbb76c12SGreg Roach return I18N::translate('Family book'); 81168ff6f3Sric2016 } 82168ff6f3Sric2016 8349a243cbSGreg Roach public function description(): string 84c1010edaSGreg Roach { 85bbb76c12SGreg Roach /* I18N: Description of the “FamilyBookChart” module */ 86bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors and descendants, as a family book.'); 87168ff6f3Sric2016 } 88168ff6f3Sric2016 89168ff6f3Sric2016 /** 90377a2979SGreg Roach * CSS class for the URL. 91377a2979SGreg Roach * 92377a2979SGreg Roach * @return string 93377a2979SGreg Roach */ 94377a2979SGreg Roach public function chartMenuClass(): string 95377a2979SGreg Roach { 96377a2979SGreg Roach return 'menu-chart-familybook'; 97377a2979SGreg Roach } 98377a2979SGreg Roach 99377a2979SGreg Roach /** 1004eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1014eb71cfaSGreg Roach */ 102*1ff45046SGreg Roach public function chartBoxMenu(Individual $individual): Menu|null 103c1010edaSGreg Roach { 104e6562982SGreg Roach return $this->chartMenu($individual); 105e6562982SGreg Roach } 106e6562982SGreg Roach 107e6562982SGreg Roach /** 108e6562982SGreg Roach * The title for a specific instance of this chart. 109e6562982SGreg Roach * 110e6562982SGreg Roach * @param Individual $individual 111e6562982SGreg Roach * 112e6562982SGreg Roach * @return string 113e6562982SGreg Roach */ 114e6562982SGreg Roach public function chartTitle(Individual $individual): string 115e6562982SGreg Roach { 116e6562982SGreg Roach /* I18N: %s is an individual’s name */ 11739ca88baSGreg Roach return I18N::translate('Family book of %s', $individual->fullName()); 118e6562982SGreg Roach } 119e6562982SGreg Roach 120e6562982SGreg Roach /** 12171378461SGreg Roach * The URL for a page showing chart options. 122389266c0SGreg Roach * 12371378461SGreg Roach * @param Individual $individual 12476d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 12571378461SGreg Roach * 12671378461SGreg Roach * @return string 12771378461SGreg Roach */ 12871378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 12971378461SGreg Roach { 13072f04adfSGreg Roach return route(static::class, [ 13171378461SGreg Roach 'xref' => $individual->xref(), 13271378461SGreg Roach 'tree' => $individual->tree()->name(), 13371378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 13471378461SGreg Roach } 13571378461SGreg Roach 13671378461SGreg Roach /** 1376ccdf4f0SGreg Roach * @param ServerRequestInterface $request 138389266c0SGreg Roach * 1396ccdf4f0SGreg Roach * @return ResponseInterface 140389266c0SGreg Roach */ 14171378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 142389266c0SGreg Roach { 143b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 144b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 145b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 146b55cbc6bSGreg Roach $book_size = Validator::attributes($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'); 147b55cbc6bSGreg Roach $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 148beda9b12SGreg Roach $spouses = Validator::attributes($request)->boolean('spouses', false); 149b55cbc6bSGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 150389266c0SGreg Roach 15171378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 15271378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 15372f04adfSGreg Roach return redirect(route(static::class, [ 1544ea62551SGreg Roach 'tree' => $tree->name(), 155158900c2SGreg Roach 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 156158900c2SGreg Roach 'book_size' => Validator::parsedBody($request)->isBetween(self::MINIMUM_BOOK_SIZE, self::MAXIMUM_BOOK_SIZE)->integer('book_size'), 157158900c2SGreg Roach 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 158beda9b12SGreg Roach 'spouses' => Validator::parsedBody($request)->boolean('spouses', false), 15971378461SGreg Roach ])); 16071378461SGreg Roach } 16171378461SGreg Roach 162ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 163389266c0SGreg Roach 164b55cbc6bSGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 165b55cbc6bSGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 166389266c0SGreg Roach 167b55cbc6bSGreg Roach if ($ajax) { 16871378461SGreg Roach $this->layout = 'layouts/ajax'; 16971378461SGreg Roach 17071378461SGreg Roach return $this->viewResponse('modules/family-book-chart/chart', [ 17171378461SGreg Roach 'individual' => $individual, 17271378461SGreg Roach 'generations' => $generations, 17371378461SGreg Roach 'book_size' => $book_size, 17471378461SGreg Roach 'spouses' => $spouses, 17571378461SGreg Roach ]); 176389266c0SGreg Roach } 177389266c0SGreg Roach 178389266c0SGreg Roach $ajax_url = $this->chartUrl($individual, [ 1799b5537c3SGreg Roach 'ajax' => true, 180389266c0SGreg Roach 'book_size' => $book_size, 181389266c0SGreg Roach 'generations' => $generations, 18271378461SGreg Roach 'spouses' => $spouses, 183389266c0SGreg Roach ]); 184389266c0SGreg Roach 1859b5537c3SGreg Roach return $this->viewResponse('modules/family-book-chart/page', [ 186389266c0SGreg Roach 'ajax_url' => $ajax_url, 187389266c0SGreg Roach 'book_size' => $book_size, 188389266c0SGreg Roach 'generations' => $generations, 189389266c0SGreg Roach 'individual' => $individual, 190b55cbc6bSGreg Roach 'maximum_book_size' => self::MAXIMUM_BOOK_SIZE, 191b55cbc6bSGreg Roach 'minimum_book_size' => self::MINIMUM_BOOK_SIZE, 192e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 193e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 19471378461SGreg Roach 'module' => $this->name(), 19571378461SGreg Roach 'spouses' => $spouses, 196389266c0SGreg Roach 'title' => $this->chartTitle($individual), 197ef5d23f1SGreg Roach 'tree' => $tree, 198389266c0SGreg Roach ]); 199389266c0SGreg Roach } 200168ff6f3Sric2016} 201