1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 Aura\Router\RouterContainer; 2371378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 2454452b04SGreg Roachuse Fisharebest\Webtrees\Auth; 25168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 26168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 2873d58381SGreg Roachuse Fisharebest\Webtrees\Registry; 2954452b04SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 304ea62551SGreg Roachuse Fisharebest\Webtrees\Tree; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3371378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3412662bfbSGreg Roach 359e18e23bSGreg Roachuse function app; 369e18e23bSGreg Roachuse function assert; 37ddeb3354SGreg Roachuse function is_string; 3871378461SGreg Roachuse function max; 3971378461SGreg Roachuse function min; 4071378461SGreg Roachuse function route; 41168ff6f3Sric2016 42168ff6f3Sric2016/** 43168ff6f3Sric2016 * Class DescendancyChartModule 44168ff6f3Sric2016 */ 4571378461SGreg Roachclass DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 46c1010edaSGreg Roach{ 4749a243cbSGreg Roach use ModuleChartTrait; 4849a243cbSGreg Roach 4972f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/descendants-{style}-{generations}/{xref}'; 5071378461SGreg Roach 5154452b04SGreg Roach // Chart styles 5231e26437SGreg Roach public const CHART_STYLE_TREE = 'tree'; 5331e26437SGreg Roach public const CHART_STYLE_INDIVIDUALS = 'individuals'; 5431e26437SGreg Roach public const CHART_STYLE_FAMILIES = 'families'; 5554452b04SGreg Roach 5654452b04SGreg Roach // Defaults 570f1e0f10SGreg Roach public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 58677aaceaSGreg Roach public const DEFAULT_GENERATIONS = '3'; 5971378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 6071378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 6171378461SGreg Roach 'style' => self::DEFAULT_STYLE, 6271378461SGreg Roach ]; 63e759aebbSGreg Roach 64e759aebbSGreg Roach // Limits 6571378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 6671378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 6754452b04SGreg Roach 6843f2f523SGreg Roach private ChartService $chart_service; 6957ab2231SGreg Roach 7057ab2231SGreg Roach /** 7157ab2231SGreg Roach * DescendancyChartModule constructor. 7257ab2231SGreg Roach * 7357ab2231SGreg Roach * @param ChartService $chart_service 7457ab2231SGreg Roach */ 753976b470SGreg Roach public function __construct(ChartService $chart_service) 763976b470SGreg Roach { 7757ab2231SGreg Roach $this->chart_service = $chart_service; 7857ab2231SGreg Roach } 7957ab2231SGreg Roach 80168ff6f3Sric2016 /** 8171378461SGreg Roach * Initialization. 8271378461SGreg Roach * 839e18e23bSGreg Roach * @return void 8471378461SGreg Roach */ 859e18e23bSGreg Roach public function boot(): void 8671378461SGreg Roach { 879e18e23bSGreg Roach $router_container = app(RouterContainer::class); 889e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 899e18e23bSGreg Roach 9071378461SGreg Roach $router_container->getMap() 9172f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 9271378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST) 9371378461SGreg Roach ->tokens([ 9471378461SGreg Roach 'generations' => '\d+', 9571378461SGreg Roach 'style' => implode('|', array_keys($this->styles())), 9671378461SGreg Roach ]); 9771378461SGreg Roach } 9871378461SGreg Roach 9971378461SGreg Roach /** 1000cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 101168ff6f3Sric2016 * 102168ff6f3Sric2016 * @return string 103168ff6f3Sric2016 */ 10449a243cbSGreg Roach public function title(): string 105c1010edaSGreg Roach { 106bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 107bbb76c12SGreg Roach return I18N::translate('Descendants'); 108168ff6f3Sric2016 } 109168ff6f3Sric2016 110168ff6f3Sric2016 /** 111168ff6f3Sric2016 * A sentence describing what this module does. 112168ff6f3Sric2016 * 113168ff6f3Sric2016 * @return string 114168ff6f3Sric2016 */ 11549a243cbSGreg Roach public function description(): string 116c1010edaSGreg Roach { 117bbb76c12SGreg Roach /* I18N: Description of the “DescendancyChart” module */ 118bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s descendants.'); 119168ff6f3Sric2016 } 120168ff6f3Sric2016 121168ff6f3Sric2016 /** 122377a2979SGreg Roach * CSS class for the URL. 123377a2979SGreg Roach * 124377a2979SGreg Roach * @return string 125377a2979SGreg Roach */ 126377a2979SGreg Roach public function chartMenuClass(): string 127377a2979SGreg Roach { 128377a2979SGreg Roach return 'menu-chart-descendants'; 129377a2979SGreg Roach } 130377a2979SGreg Roach 131377a2979SGreg Roach /** 1324eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1334eb71cfaSGreg Roach * 13460bc3e3fSGreg Roach * @param Individual $individual 13560bc3e3fSGreg Roach * 1364eb71cfaSGreg Roach * @return Menu|null 1374eb71cfaSGreg Roach */ 138377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 139c1010edaSGreg Roach { 140e6562982SGreg Roach return $this->chartMenu($individual); 141e6562982SGreg Roach } 142e6562982SGreg Roach 143e6562982SGreg Roach /** 144e6562982SGreg Roach * The title for a specific instance of this chart. 145e6562982SGreg Roach * 146e6562982SGreg Roach * @param Individual $individual 147e6562982SGreg Roach * 148e6562982SGreg Roach * @return string 149e6562982SGreg Roach */ 150e6562982SGreg Roach public function chartTitle(Individual $individual): string 151e6562982SGreg Roach { 152e6562982SGreg Roach /* I18N: %s is an individual’s name */ 15339ca88baSGreg Roach return I18N::translate('Descendants of %s', $individual->fullName()); 154e6562982SGreg Roach } 155e6562982SGreg Roach 156e6562982SGreg Roach /** 15771378461SGreg Roach * The URL for a page showing chart options. 15854452b04SGreg Roach * 15971378461SGreg Roach * @param Individual $individual 160*76d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 16171378461SGreg Roach * 16271378461SGreg Roach * @return string 16371378461SGreg Roach */ 16471378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 16571378461SGreg Roach { 16672f04adfSGreg Roach return route(static::class, [ 16771378461SGreg Roach 'tree' => $individual->tree()->name(), 16871378461SGreg Roach 'xref' => $individual->xref(), 16971378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 17071378461SGreg Roach } 17171378461SGreg Roach 17271378461SGreg Roach /** 1736ccdf4f0SGreg Roach * @param ServerRequestInterface $request 17454452b04SGreg Roach * 1756ccdf4f0SGreg Roach * @return ResponseInterface 17654452b04SGreg Roach */ 17771378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 17854452b04SGreg Roach { 17957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 1804ea62551SGreg Roach assert($tree instanceof Tree); 1814ea62551SGreg Roach 18271378461SGreg Roach $xref = $request->getAttribute('xref'); 183ddeb3354SGreg Roach assert(is_string($xref)); 184ddeb3354SGreg Roach 1856b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 1863c3fd0a5SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 187ddeb3354SGreg Roach 188ddeb3354SGreg Roach $user = $request->getAttribute('user'); 18971378461SGreg Roach $style = $request->getAttribute('style'); 19071378461SGreg Roach $generations = (int) $request->getAttribute('generations'); 1910b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 19254452b04SGreg Roach 19371378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 19471378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 195b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 196b46c87bdSGreg Roach 19772f04adfSGreg Roach return redirect(route(static::class, [ 1984ea62551SGreg Roach 'tree' => $tree->name(), 199b46c87bdSGreg Roach 'xref' => $params['xref'], 200b46c87bdSGreg Roach 'style' => $params['style'], 201b46c87bdSGreg Roach 'generations' => $params['generations'], 20271378461SGreg Roach ])); 20371378461SGreg Roach } 20471378461SGreg Roach 205ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 20654452b04SGreg Roach 207e759aebbSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 208e759aebbSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 20954452b04SGreg Roach 2100b93976aSGreg Roach if ($ajax === '1') { 21171378461SGreg Roach $this->layout = 'layouts/ajax'; 21271378461SGreg Roach 21371378461SGreg Roach switch ($style) { 21471378461SGreg Roach case self::CHART_STYLE_TREE: 21571378461SGreg Roach return $this->viewResponse('modules/descendancy_chart/tree', [ 21671378461SGreg Roach 'individual' => $individual, 21771378461SGreg Roach 'generations' => $generations, 21871378461SGreg Roach 'daboville' => '1', 21971378461SGreg Roach ]); 22071378461SGreg Roach 22171378461SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 2229bbda1e6SGreg Roach $individuals = $this->chart_service->descendants($individual, $generations - 1); 2239bbda1e6SGreg Roach 22471378461SGreg Roach return $this->viewResponse('lists/individuals-table', [ 2259bbda1e6SGreg Roach 'individuals' => $individuals, 22671378461SGreg Roach 'sosa' => false, 22771378461SGreg Roach 'tree' => $tree, 22871378461SGreg Roach ]); 22971378461SGreg Roach 23071378461SGreg Roach case self::CHART_STYLE_FAMILIES: 23171378461SGreg Roach $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 23271378461SGreg Roach 2339bbda1e6SGreg Roach return $this->viewResponse('lists/families-table', [ 2349bbda1e6SGreg Roach 'families' => $families, 2359bbda1e6SGreg Roach 'tree' => $tree, 2369bbda1e6SGreg Roach ]); 23771378461SGreg Roach } 23854452b04SGreg Roach } 23954452b04SGreg Roach 24054452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 24154452b04SGreg Roach 'generations' => $generations, 24271378461SGreg Roach 'style' => $style, 2439b5537c3SGreg Roach 'ajax' => true, 24454452b04SGreg Roach ]); 24554452b04SGreg Roach 2469b5537c3SGreg Roach return $this->viewResponse('modules/descendancy_chart/page', [ 24754452b04SGreg Roach 'ajax_url' => $ajax_url, 24871378461SGreg Roach 'style' => $style, 24971378461SGreg Roach 'styles' => $this->styles(), 250e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 25154452b04SGreg Roach 'generations' => $generations, 25254452b04SGreg Roach 'individual' => $individual, 253e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 254e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 25571378461SGreg Roach 'module' => $this->name(), 25654452b04SGreg Roach 'title' => $this->chartTitle($individual), 257ef5d23f1SGreg Roach 'tree' => $tree, 25854452b04SGreg Roach ]); 25954452b04SGreg Roach } 26054452b04SGreg Roach 26154452b04SGreg Roach /** 26254452b04SGreg Roach * This chart can display its output in a number of styles 26354452b04SGreg Roach * 26424f2a3afSGreg Roach * @return array<string> 26554452b04SGreg Roach */ 26671378461SGreg Roach protected function styles(): array 26754452b04SGreg Roach { 26854452b04SGreg Roach return [ 2690f1e0f10SGreg Roach self::CHART_STYLE_TREE => I18N::translate('Tree'), 27054452b04SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 27154452b04SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 27254452b04SGreg Roach ]; 273e6562982SGreg Roach } 274168ff6f3Sric2016} 275