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; 23e539f5c6SGreg Roachuse Fisharebest\Webtrees\Auth; 24168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 25168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Registry; 28e539f5c6SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3271378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3312662bfbSGreg Roach 3471378461SGreg Roachuse function route; 35168ff6f3Sric2016 36168ff6f3Sric2016/** 37168ff6f3Sric2016 * Class AncestorsChartModule 38168ff6f3Sric2016 */ 3971378461SGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 40c1010edaSGreg Roach{ 4149a243cbSGreg Roach use ModuleChartTrait; 4249a243cbSGreg Roach 4372f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/ancestors-{style}-{generations}/{xref}'; 4471378461SGreg Roach 45e539f5c6SGreg Roach // Chart styles 4671378461SGreg Roach public const CHART_STYLE_TREE = 'tree'; 4771378461SGreg Roach public const CHART_STYLE_INDIVIDUALS = 'individuals'; 4871378461SGreg Roach public const CHART_STYLE_FAMILIES = 'families'; 49e539f5c6SGreg Roach 50e539f5c6SGreg Roach // Defaults 51266e9c61SGreg Roach public const DEFAULT_GENERATIONS = '4'; 52266e9c61SGreg Roach public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 5371378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 5471378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 5571378461SGreg Roach 'style' => self::DEFAULT_STYLE, 5671378461SGreg Roach ]; 57e759aebbSGreg Roach 58e759aebbSGreg Roach // Limits 59e759aebbSGreg Roach protected const MINIMUM_GENERATIONS = 2; 60e759aebbSGreg Roach protected const MAXIMUM_GENERATIONS = 10; 61e539f5c6SGreg Roach 6243f2f523SGreg Roach private ChartService $chart_service; 6357ab2231SGreg Roach 6457ab2231SGreg Roach /** 6557ab2231SGreg Roach * @param ChartService $chart_service 6657ab2231SGreg Roach */ 673976b470SGreg Roach public function __construct(ChartService $chart_service) 683976b470SGreg Roach { 6957ab2231SGreg Roach $this->chart_service = $chart_service; 7057ab2231SGreg Roach } 7157ab2231SGreg Roach 72168ff6f3Sric2016 /** 7371378461SGreg Roach * Initialization. 7471378461SGreg Roach * 759e18e23bSGreg Roach * @return void 7671378461SGreg Roach */ 779e18e23bSGreg Roach public function boot(): void 7871378461SGreg Roach { 79158900c2SGreg Roach Registry::routeFactory()->routeMap() 8072f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 8171378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST) 8271378461SGreg Roach ->tokens([ 8371378461SGreg Roach 'generations' => '\d+', 8471378461SGreg Roach 'style' => implode('|', array_keys($this->styles())), 8571378461SGreg Roach ]); 8671378461SGreg Roach } 8771378461SGreg Roach 8871378461SGreg Roach /** 890cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 90168ff6f3Sric2016 * 91168ff6f3Sric2016 * @return string 92168ff6f3Sric2016 */ 9349a243cbSGreg Roach public function title(): string 94c1010edaSGreg Roach { 95bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 96bbb76c12SGreg Roach return I18N::translate('Ancestors'); 97168ff6f3Sric2016 } 98168ff6f3Sric2016 9949a243cbSGreg Roach public function description(): string 100c1010edaSGreg Roach { 101bbb76c12SGreg Roach /* I18N: Description of the “AncestorsChart” module */ 102bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors.'); 103168ff6f3Sric2016 } 104168ff6f3Sric2016 105168ff6f3Sric2016 /** 106377a2979SGreg Roach * CSS class for the URL. 107377a2979SGreg Roach * 108377a2979SGreg Roach * @return string 109377a2979SGreg Roach */ 110377a2979SGreg Roach public function chartMenuClass(): string 111377a2979SGreg Roach { 112377a2979SGreg Roach return 'menu-chart-ancestry'; 113377a2979SGreg Roach } 114377a2979SGreg Roach 115377a2979SGreg Roach /** 1164eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1174eb71cfaSGreg Roach */ 118*1ff45046SGreg Roach public function chartBoxMenu(Individual $individual): Menu|null 119c1010edaSGreg Roach { 120e6562982SGreg Roach return $this->chartMenu($individual); 121e6562982SGreg Roach } 122e6562982SGreg Roach 123e6562982SGreg Roach /** 124e6562982SGreg Roach * The title for a specific instance of this chart. 125e6562982SGreg Roach * 126e6562982SGreg Roach * @param Individual $individual 127e6562982SGreg Roach * 128e6562982SGreg Roach * @return string 129e6562982SGreg Roach */ 130e6562982SGreg Roach public function chartTitle(Individual $individual): string 131e6562982SGreg Roach { 132e6562982SGreg Roach /* I18N: %s is an individual’s name */ 13339ca88baSGreg Roach return I18N::translate('Ancestors of %s', $individual->fullName()); 134e6562982SGreg Roach } 135e6562982SGreg Roach 136e6562982SGreg Roach /** 13771378461SGreg Roach * The URL for a page showing chart options. 138e539f5c6SGreg Roach * 13971378461SGreg Roach * @param Individual $individual 140d8809d62SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 14171378461SGreg Roach * 14271378461SGreg Roach * @return string 14371378461SGreg Roach */ 14471378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 14571378461SGreg Roach { 14672f04adfSGreg Roach return route(static::class, [ 14771378461SGreg Roach 'xref' => $individual->xref(), 14871378461SGreg Roach 'tree' => $individual->tree()->name(), 14971378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 15071378461SGreg Roach } 15171378461SGreg Roach 15271378461SGreg Roach /** 1536ccdf4f0SGreg Roach * @param ServerRequestInterface $request 154e539f5c6SGreg Roach * 1556ccdf4f0SGreg Roach * @return ResponseInterface 156e539f5c6SGreg Roach */ 15771378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 158e539f5c6SGreg Roach { 159b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 160b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 161158900c2SGreg Roach $style = Validator::attributes($request)->isInArrayKeys($this->styles())->string('style'); 162b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 163b55cbc6bSGreg Roach $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 164b55cbc6bSGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 1654ea62551SGreg Roach 166b55cbc6bSGreg Roach // Convert POST requests into GET requests for pretty URLs. 167b55cbc6bSGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 168b55cbc6bSGreg Roach return redirect(route(static::class, [ 169b55cbc6bSGreg Roach 'tree' => $tree->name(), 170158900c2SGreg Roach 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 171158900c2SGreg Roach 'style' => Validator::parsedBody($request)->isInArrayKeys($this->styles())->string('style'), 172158900c2SGreg Roach 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 173b55cbc6bSGreg Roach ])); 174b55cbc6bSGreg Roach } 175b55cbc6bSGreg Roach 176b55cbc6bSGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 177ddeb3354SGreg Roach 1786b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 1793c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 180ddeb3354SGreg Roach 181b55cbc6bSGreg Roach if ($ajax) { 182e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 183e539f5c6SGreg Roach 18471378461SGreg Roach $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations); 18571378461SGreg Roach 18671378461SGreg Roach switch ($style) { 18771378461SGreg Roach default: 18871378461SGreg Roach case self::CHART_STYLE_TREE: 18971378461SGreg Roach return $this->viewResponse('modules/ancestors-chart/tree', [ 19071378461SGreg Roach 'individual' => $individual, 1911afbbc50SGreg Roach 'parents' => $individual->childFamilies()->first(), 19271378461SGreg Roach 'generations' => $generations, 19371378461SGreg Roach 'sosa' => 1, 19471378461SGreg Roach ]); 19571378461SGreg Roach 19671378461SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 197e539f5c6SGreg Roach return $this->viewResponse('lists/individuals-table', [ 198e539f5c6SGreg Roach 'individuals' => $ancestors, 199e539f5c6SGreg Roach 'sosa' => true, 200e539f5c6SGreg Roach 'tree' => $tree, 201e539f5c6SGreg Roach ]); 202e539f5c6SGreg Roach 20371378461SGreg Roach case self::CHART_STYLE_FAMILIES: 204e539f5c6SGreg Roach $families = []; 20571378461SGreg Roach 206e539f5c6SGreg Roach foreach ($ancestors as $individual) { 20739ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 208e539f5c6SGreg Roach $families[$family->xref()] = $family; 209e539f5c6SGreg Roach } 210e539f5c6SGreg Roach } 211e539f5c6SGreg Roach 212ef5d23f1SGreg Roach return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]); 213e539f5c6SGreg Roach } 21471378461SGreg Roach } 21571378461SGreg Roach 21671378461SGreg Roach $ajax_url = $this->chartUrl($individual, [ 21771378461SGreg Roach 'ajax' => true, 21871378461SGreg Roach 'generations' => $generations, 21971378461SGreg Roach 'style' => $style, 22071378461SGreg Roach 'xref' => $xref, 22171378461SGreg Roach ]); 22271378461SGreg Roach 22371378461SGreg Roach return $this->viewResponse('modules/ancestors-chart/page', [ 22471378461SGreg Roach 'ajax_url' => $ajax_url, 22571378461SGreg Roach 'generations' => $generations, 22671378461SGreg Roach 'individual' => $individual, 22771378461SGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 22871378461SGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 22971378461SGreg Roach 'module' => $this->name(), 23071378461SGreg Roach 'style' => $style, 23171378461SGreg Roach 'styles' => $this->styles(), 23271378461SGreg Roach 'title' => $this->chartTitle($individual), 233ef5d23f1SGreg Roach 'tree' => $tree, 23471378461SGreg Roach ]); 23571378461SGreg Roach } 236e539f5c6SGreg Roach 237e539f5c6SGreg Roach /** 238e539f5c6SGreg Roach * This chart can display its output in a number of styles 239e539f5c6SGreg Roach * 24024f2a3afSGreg Roach * @return array<string> 241e539f5c6SGreg Roach */ 24271378461SGreg Roach protected function styles(): array 243e539f5c6SGreg Roach { 244e539f5c6SGreg Roach return [ 2450f1e0f10SGreg Roach self::CHART_STYLE_TREE => I18N::translate('Tree'), 246e539f5c6SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 247e539f5c6SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 248e539f5c6SGreg Roach ]; 249e539f5c6SGreg Roach } 250168ff6f3Sric2016} 251