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; 30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 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; 3771378461SGreg Roachuse function max; 3871378461SGreg Roachuse function min; 3971378461SGreg Roachuse function route; 40168ff6f3Sric2016 41168ff6f3Sric2016/** 42168ff6f3Sric2016 * Class DescendancyChartModule 43168ff6f3Sric2016 */ 4471378461SGreg Roachclass DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 45c1010edaSGreg Roach{ 4649a243cbSGreg Roach use ModuleChartTrait; 4749a243cbSGreg Roach 4872f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/descendants-{style}-{generations}/{xref}'; 4971378461SGreg Roach 5054452b04SGreg Roach // Chart styles 5131e26437SGreg Roach public const CHART_STYLE_TREE = 'tree'; 5231e26437SGreg Roach public const CHART_STYLE_INDIVIDUALS = 'individuals'; 5331e26437SGreg Roach public const CHART_STYLE_FAMILIES = 'families'; 5454452b04SGreg Roach 5554452b04SGreg Roach // Defaults 560f1e0f10SGreg Roach public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 57677aaceaSGreg Roach public const DEFAULT_GENERATIONS = '3'; 5871378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 5971378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 6071378461SGreg Roach 'style' => self::DEFAULT_STYLE, 6171378461SGreg Roach ]; 62e759aebbSGreg Roach 63e759aebbSGreg Roach // Limits 6471378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 6571378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 6654452b04SGreg Roach 6743f2f523SGreg Roach private ChartService $chart_service; 6857ab2231SGreg Roach 6957ab2231SGreg Roach /** 7057ab2231SGreg Roach * DescendancyChartModule constructor. 7157ab2231SGreg Roach * 7257ab2231SGreg Roach * @param ChartService $chart_service 7357ab2231SGreg Roach */ 743976b470SGreg Roach public function __construct(ChartService $chart_service) 753976b470SGreg Roach { 7657ab2231SGreg Roach $this->chart_service = $chart_service; 7757ab2231SGreg Roach } 7857ab2231SGreg Roach 79168ff6f3Sric2016 /** 8071378461SGreg Roach * Initialization. 8171378461SGreg Roach * 829e18e23bSGreg Roach * @return void 8371378461SGreg Roach */ 849e18e23bSGreg Roach public function boot(): void 8571378461SGreg Roach { 86*158900c2SGreg Roach Registry::routeFactory()->routeMap() 8772f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 88*158900c2SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 8971378461SGreg Roach } 9071378461SGreg Roach 9171378461SGreg Roach /** 920cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 93168ff6f3Sric2016 * 94168ff6f3Sric2016 * @return string 95168ff6f3Sric2016 */ 9649a243cbSGreg Roach public function title(): string 97c1010edaSGreg Roach { 98bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 99bbb76c12SGreg Roach return I18N::translate('Descendants'); 100168ff6f3Sric2016 } 101168ff6f3Sric2016 102168ff6f3Sric2016 /** 103168ff6f3Sric2016 * A sentence describing what this module does. 104168ff6f3Sric2016 * 105168ff6f3Sric2016 * @return string 106168ff6f3Sric2016 */ 10749a243cbSGreg Roach public function description(): string 108c1010edaSGreg Roach { 109bbb76c12SGreg Roach /* I18N: Description of the “DescendancyChart” module */ 110bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s descendants.'); 111168ff6f3Sric2016 } 112168ff6f3Sric2016 113168ff6f3Sric2016 /** 114377a2979SGreg Roach * CSS class for the URL. 115377a2979SGreg Roach * 116377a2979SGreg Roach * @return string 117377a2979SGreg Roach */ 118377a2979SGreg Roach public function chartMenuClass(): string 119377a2979SGreg Roach { 120377a2979SGreg Roach return 'menu-chart-descendants'; 121377a2979SGreg Roach } 122377a2979SGreg Roach 123377a2979SGreg Roach /** 1244eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1254eb71cfaSGreg Roach * 12660bc3e3fSGreg Roach * @param Individual $individual 12760bc3e3fSGreg Roach * 1284eb71cfaSGreg Roach * @return Menu|null 1294eb71cfaSGreg Roach */ 130377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 131c1010edaSGreg Roach { 132e6562982SGreg Roach return $this->chartMenu($individual); 133e6562982SGreg Roach } 134e6562982SGreg Roach 135e6562982SGreg Roach /** 136e6562982SGreg Roach * The title for a specific instance of this chart. 137e6562982SGreg Roach * 138e6562982SGreg Roach * @param Individual $individual 139e6562982SGreg Roach * 140e6562982SGreg Roach * @return string 141e6562982SGreg Roach */ 142e6562982SGreg Roach public function chartTitle(Individual $individual): string 143e6562982SGreg Roach { 144e6562982SGreg Roach /* I18N: %s is an individual’s name */ 14539ca88baSGreg Roach return I18N::translate('Descendants of %s', $individual->fullName()); 146e6562982SGreg Roach } 147e6562982SGreg Roach 148e6562982SGreg Roach /** 14971378461SGreg Roach * The URL for a page showing chart options. 15054452b04SGreg Roach * 15171378461SGreg Roach * @param Individual $individual 15276d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 15371378461SGreg Roach * 15471378461SGreg Roach * @return string 15571378461SGreg Roach */ 15671378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 15771378461SGreg Roach { 15872f04adfSGreg Roach return route(static::class, [ 15971378461SGreg Roach 'tree' => $individual->tree()->name(), 16071378461SGreg Roach 'xref' => $individual->xref(), 16171378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 16271378461SGreg Roach } 16371378461SGreg Roach 16471378461SGreg Roach /** 1656ccdf4f0SGreg Roach * @param ServerRequestInterface $request 16654452b04SGreg Roach * 1676ccdf4f0SGreg Roach * @return ResponseInterface 16854452b04SGreg Roach */ 16971378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 17054452b04SGreg Roach { 171b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 172b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 173b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 174*158900c2SGreg Roach $style = Validator::attributes($request)->isInArrayKeys($this->styles())->string('style'); 175b55cbc6bSGreg Roach $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 176*158900c2SGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 17754452b04SGreg Roach 17871378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 17971378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 18072f04adfSGreg Roach return redirect(route(static::class, [ 1814ea62551SGreg Roach 'tree' => $tree->name(), 182*158900c2SGreg Roach 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 183*158900c2SGreg Roach 'style' => Validator::parsedBody($request)->isInArrayKeys($this->styles())->string('style'), 184*158900c2SGreg Roach 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 18571378461SGreg Roach ])); 18671378461SGreg Roach } 18771378461SGreg Roach 188ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 18954452b04SGreg Roach 190b55cbc6bSGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 191b55cbc6bSGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 19254452b04SGreg Roach 193b55cbc6bSGreg Roach if ($ajax) { 19471378461SGreg Roach $this->layout = 'layouts/ajax'; 19571378461SGreg Roach 19671378461SGreg Roach switch ($style) { 19771378461SGreg Roach case self::CHART_STYLE_TREE: 19871378461SGreg Roach return $this->viewResponse('modules/descendancy_chart/tree', [ 19971378461SGreg Roach 'individual' => $individual, 20071378461SGreg Roach 'generations' => $generations, 20171378461SGreg Roach 'daboville' => '1', 20271378461SGreg Roach ]); 20371378461SGreg Roach 20471378461SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 2059bbda1e6SGreg Roach $individuals = $this->chart_service->descendants($individual, $generations - 1); 2069bbda1e6SGreg Roach 20771378461SGreg Roach return $this->viewResponse('lists/individuals-table', [ 2089bbda1e6SGreg Roach 'individuals' => $individuals, 20971378461SGreg Roach 'sosa' => false, 21071378461SGreg Roach 'tree' => $tree, 21171378461SGreg Roach ]); 21271378461SGreg Roach 21371378461SGreg Roach case self::CHART_STYLE_FAMILIES: 21471378461SGreg Roach $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 21571378461SGreg Roach 2169bbda1e6SGreg Roach return $this->viewResponse('lists/families-table', [ 2179bbda1e6SGreg Roach 'families' => $families, 2189bbda1e6SGreg Roach 'tree' => $tree, 2199bbda1e6SGreg Roach ]); 22071378461SGreg Roach } 22154452b04SGreg Roach } 22254452b04SGreg Roach 22354452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 22454452b04SGreg Roach 'generations' => $generations, 22571378461SGreg Roach 'style' => $style, 2269b5537c3SGreg Roach 'ajax' => true, 22754452b04SGreg Roach ]); 22854452b04SGreg Roach 2299b5537c3SGreg Roach return $this->viewResponse('modules/descendancy_chart/page', [ 23054452b04SGreg Roach 'ajax_url' => $ajax_url, 23171378461SGreg Roach 'style' => $style, 23271378461SGreg Roach 'styles' => $this->styles(), 233e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 23454452b04SGreg Roach 'generations' => $generations, 23554452b04SGreg Roach 'individual' => $individual, 236e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 237e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 23871378461SGreg Roach 'module' => $this->name(), 23954452b04SGreg Roach 'title' => $this->chartTitle($individual), 240ef5d23f1SGreg Roach 'tree' => $tree, 24154452b04SGreg Roach ]); 24254452b04SGreg Roach } 24354452b04SGreg Roach 24454452b04SGreg Roach /** 24554452b04SGreg Roach * This chart can display its output in a number of styles 24654452b04SGreg Roach * 24724f2a3afSGreg Roach * @return array<string> 24854452b04SGreg Roach */ 24971378461SGreg Roach protected function styles(): array 25054452b04SGreg Roach { 25154452b04SGreg Roach return [ 2520f1e0f10SGreg Roach self::CHART_STYLE_TREE => I18N::translate('Tree'), 25354452b04SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 25454452b04SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 25554452b04SGreg Roach ]; 256e6562982SGreg Roach } 257168ff6f3Sric2016} 258