1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 5*d11be702SGreg 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; 2354452b04SGreg Roachuse Fisharebest\Webtrees\Auth; 24168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 25168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 2773d58381SGreg Roachuse Fisharebest\Webtrees\Registry; 2854452b04SGreg 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 DescendancyChartModule 38168ff6f3Sric2016 */ 3971378461SGreg Roachclass DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 40c1010edaSGreg Roach{ 4149a243cbSGreg Roach use ModuleChartTrait; 4249a243cbSGreg Roach 4372f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/descendants-{style}-{generations}/{xref}'; 4471378461SGreg Roach 4554452b04SGreg Roach // Chart styles 4631e26437SGreg Roach public const CHART_STYLE_TREE = 'tree'; 4731e26437SGreg Roach public const CHART_STYLE_INDIVIDUALS = 'individuals'; 4831e26437SGreg Roach public const CHART_STYLE_FAMILIES = 'families'; 4954452b04SGreg Roach 5054452b04SGreg Roach // Defaults 510f1e0f10SGreg Roach public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 52677aaceaSGreg Roach public const DEFAULT_GENERATIONS = '3'; 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 5971378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 6071378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 6154452b04SGreg Roach 6243f2f523SGreg Roach private ChartService $chart_service; 6357ab2231SGreg Roach 6457ab2231SGreg Roach /** 6557ab2231SGreg Roach * DescendancyChartModule constructor. 6657ab2231SGreg Roach * 6757ab2231SGreg Roach * @param ChartService $chart_service 6857ab2231SGreg Roach */ 693976b470SGreg Roach public function __construct(ChartService $chart_service) 703976b470SGreg Roach { 7157ab2231SGreg Roach $this->chart_service = $chart_service; 7257ab2231SGreg Roach } 7357ab2231SGreg Roach 74168ff6f3Sric2016 /** 7571378461SGreg Roach * Initialization. 7671378461SGreg Roach * 779e18e23bSGreg Roach * @return void 7871378461SGreg Roach */ 799e18e23bSGreg Roach public function boot(): void 8071378461SGreg Roach { 81158900c2SGreg Roach Registry::routeFactory()->routeMap() 8272f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 83158900c2SGreg Roach ->allows(RequestMethodInterface::METHOD_POST); 8471378461SGreg Roach } 8571378461SGreg Roach 8671378461SGreg Roach /** 870cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 88168ff6f3Sric2016 * 89168ff6f3Sric2016 * @return string 90168ff6f3Sric2016 */ 9149a243cbSGreg Roach public function title(): string 92c1010edaSGreg Roach { 93bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 94bbb76c12SGreg Roach return I18N::translate('Descendants'); 95168ff6f3Sric2016 } 96168ff6f3Sric2016 97168ff6f3Sric2016 /** 98168ff6f3Sric2016 * A sentence describing what this module does. 99168ff6f3Sric2016 * 100168ff6f3Sric2016 * @return string 101168ff6f3Sric2016 */ 10249a243cbSGreg Roach public function description(): string 103c1010edaSGreg Roach { 104bbb76c12SGreg Roach /* I18N: Description of the “DescendancyChart” module */ 105bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s descendants.'); 106168ff6f3Sric2016 } 107168ff6f3Sric2016 108168ff6f3Sric2016 /** 109377a2979SGreg Roach * CSS class for the URL. 110377a2979SGreg Roach * 111377a2979SGreg Roach * @return string 112377a2979SGreg Roach */ 113377a2979SGreg Roach public function chartMenuClass(): string 114377a2979SGreg Roach { 115377a2979SGreg Roach return 'menu-chart-descendants'; 116377a2979SGreg Roach } 117377a2979SGreg Roach 118377a2979SGreg Roach /** 1194eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1204eb71cfaSGreg Roach * 12160bc3e3fSGreg Roach * @param Individual $individual 12260bc3e3fSGreg Roach * 1234eb71cfaSGreg Roach * @return Menu|null 1244eb71cfaSGreg Roach */ 125377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 126c1010edaSGreg Roach { 127e6562982SGreg Roach return $this->chartMenu($individual); 128e6562982SGreg Roach } 129e6562982SGreg Roach 130e6562982SGreg Roach /** 131e6562982SGreg Roach * The title for a specific instance of this chart. 132e6562982SGreg Roach * 133e6562982SGreg Roach * @param Individual $individual 134e6562982SGreg Roach * 135e6562982SGreg Roach * @return string 136e6562982SGreg Roach */ 137e6562982SGreg Roach public function chartTitle(Individual $individual): string 138e6562982SGreg Roach { 139e6562982SGreg Roach /* I18N: %s is an individual’s name */ 14039ca88baSGreg Roach return I18N::translate('Descendants of %s', $individual->fullName()); 141e6562982SGreg Roach } 142e6562982SGreg Roach 143e6562982SGreg Roach /** 14471378461SGreg Roach * The URL for a page showing chart options. 14554452b04SGreg Roach * 14671378461SGreg Roach * @param Individual $individual 14776d39c55SGreg Roach * @param array<bool|int|string|array<string>|null> $parameters 14871378461SGreg Roach * 14971378461SGreg Roach * @return string 15071378461SGreg Roach */ 15171378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 15271378461SGreg Roach { 15372f04adfSGreg Roach return route(static::class, [ 15471378461SGreg Roach 'tree' => $individual->tree()->name(), 15571378461SGreg Roach 'xref' => $individual->xref(), 15671378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 15771378461SGreg Roach } 15871378461SGreg Roach 15971378461SGreg Roach /** 1606ccdf4f0SGreg Roach * @param ServerRequestInterface $request 16154452b04SGreg Roach * 1626ccdf4f0SGreg Roach * @return ResponseInterface 16354452b04SGreg Roach */ 16471378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 16554452b04SGreg Roach { 166b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 167b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 168b55cbc6bSGreg Roach $xref = Validator::attributes($request)->isXref()->string('xref'); 169158900c2SGreg Roach $style = Validator::attributes($request)->isInArrayKeys($this->styles())->string('style'); 170b55cbc6bSGreg Roach $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 171158900c2SGreg Roach $ajax = Validator::queryParams($request)->boolean('ajax', false); 17254452b04SGreg Roach 17371378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 17471378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 17572f04adfSGreg Roach return redirect(route(static::class, [ 1764ea62551SGreg Roach 'tree' => $tree->name(), 177158900c2SGreg Roach 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 178158900c2SGreg Roach 'style' => Validator::parsedBody($request)->isInArrayKeys($this->styles())->string('style'), 179158900c2SGreg Roach 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 18071378461SGreg Roach ])); 18171378461SGreg Roach } 18271378461SGreg Roach 183ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 18454452b04SGreg Roach 185b55cbc6bSGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 186b55cbc6bSGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 18754452b04SGreg Roach 188b55cbc6bSGreg Roach if ($ajax) { 18971378461SGreg Roach $this->layout = 'layouts/ajax'; 19071378461SGreg Roach 19171378461SGreg Roach switch ($style) { 19271378461SGreg Roach case self::CHART_STYLE_TREE: 19371378461SGreg Roach return $this->viewResponse('modules/descendancy_chart/tree', [ 19471378461SGreg Roach 'individual' => $individual, 19571378461SGreg Roach 'generations' => $generations, 19671378461SGreg Roach 'daboville' => '1', 19771378461SGreg Roach ]); 19871378461SGreg Roach 19971378461SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 2009bbda1e6SGreg Roach $individuals = $this->chart_service->descendants($individual, $generations - 1); 2019bbda1e6SGreg Roach 20271378461SGreg Roach return $this->viewResponse('lists/individuals-table', [ 2039bbda1e6SGreg Roach 'individuals' => $individuals, 20471378461SGreg Roach 'sosa' => false, 20571378461SGreg Roach 'tree' => $tree, 20671378461SGreg Roach ]); 20771378461SGreg Roach 20871378461SGreg Roach case self::CHART_STYLE_FAMILIES: 20971378461SGreg Roach $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 21071378461SGreg Roach 2119bbda1e6SGreg Roach return $this->viewResponse('lists/families-table', [ 2129bbda1e6SGreg Roach 'families' => $families, 2139bbda1e6SGreg Roach 'tree' => $tree, 2149bbda1e6SGreg Roach ]); 21571378461SGreg Roach } 21654452b04SGreg Roach } 21754452b04SGreg Roach 21854452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 21954452b04SGreg Roach 'generations' => $generations, 22071378461SGreg Roach 'style' => $style, 2219b5537c3SGreg Roach 'ajax' => true, 22254452b04SGreg Roach ]); 22354452b04SGreg Roach 2249b5537c3SGreg Roach return $this->viewResponse('modules/descendancy_chart/page', [ 22554452b04SGreg Roach 'ajax_url' => $ajax_url, 22671378461SGreg Roach 'style' => $style, 22771378461SGreg Roach 'styles' => $this->styles(), 228e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 22954452b04SGreg Roach 'generations' => $generations, 23054452b04SGreg Roach 'individual' => $individual, 231e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 232e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 23371378461SGreg Roach 'module' => $this->name(), 23454452b04SGreg Roach 'title' => $this->chartTitle($individual), 235ef5d23f1SGreg Roach 'tree' => $tree, 23654452b04SGreg Roach ]); 23754452b04SGreg Roach } 23854452b04SGreg Roach 23954452b04SGreg Roach /** 24054452b04SGreg Roach * This chart can display its output in a number of styles 24154452b04SGreg Roach * 24224f2a3afSGreg Roach * @return array<string> 24354452b04SGreg Roach */ 24471378461SGreg Roach protected function styles(): array 24554452b04SGreg Roach { 24654452b04SGreg Roach return [ 2470f1e0f10SGreg Roach self::CHART_STYLE_TREE => I18N::translate('Tree'), 24854452b04SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 24954452b04SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 25054452b04SGreg Roach ]; 251e6562982SGreg Roach } 252168ff6f3Sric2016} 253