1168ff6f3Sric2016<?php 23976b470SGreg Roach 3168ff6f3Sric2016/** 4168ff6f3Sric2016 * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 15168ff6f3Sric2016 * along with this program. If not, see <http://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; 2854452b04SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3171378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 329e18e23bSGreg Roachuse function app; 339e18e23bSGreg Roachuse function assert; 3471378461SGreg Roachuse function max; 3571378461SGreg Roachuse function min; 3671378461SGreg Roachuse function route; 37168ff6f3Sric2016 38168ff6f3Sric2016/** 39168ff6f3Sric2016 * Class DescendancyChartModule 40168ff6f3Sric2016 */ 4171378461SGreg Roachclass DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 42c1010edaSGreg Roach{ 4349a243cbSGreg Roach use ModuleChartTrait; 4449a243cbSGreg Roach 4571378461SGreg Roach private const ROUTE_NAME = 'descendancy-chart'; 4671378461SGreg Roach private const ROUTE_URL = '/tree/{tree}/descendants-{style}-{generations}/{xref}'; 4771378461SGreg Roach 4854452b04SGreg Roach // Chart styles 4931e26437SGreg Roach public const CHART_STYLE_TREE = 'tree'; 5031e26437SGreg Roach public const CHART_STYLE_INDIVIDUALS = 'individuals'; 5131e26437SGreg Roach public const CHART_STYLE_FAMILIES = 'families'; 5254452b04SGreg Roach 5354452b04SGreg Roach // Defaults 540f1e0f10SGreg Roach public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 55677aaceaSGreg Roach public const DEFAULT_GENERATIONS = '3'; 5671378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 5771378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 5871378461SGreg Roach 'style' => self::DEFAULT_STYLE, 5971378461SGreg Roach ]; 60e759aebbSGreg Roach 61e759aebbSGreg Roach // Limits 6271378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 6371378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 6454452b04SGreg Roach 6557ab2231SGreg Roach /** @var ChartService */ 6657ab2231SGreg Roach private $chart_service; 6757ab2231SGreg Roach 6857ab2231SGreg Roach /** 6957ab2231SGreg Roach * DescendancyChartModule constructor. 7057ab2231SGreg Roach * 7157ab2231SGreg Roach * @param ChartService $chart_service 7257ab2231SGreg Roach */ 733976b470SGreg Roach public function __construct(ChartService $chart_service) 743976b470SGreg Roach { 7557ab2231SGreg Roach $this->chart_service = $chart_service; 7657ab2231SGreg Roach } 7757ab2231SGreg Roach 78168ff6f3Sric2016 /** 7971378461SGreg Roach * Initialization. 8071378461SGreg Roach * 819e18e23bSGreg Roach * @return void 8271378461SGreg Roach */ 839e18e23bSGreg Roach public function boot(): void 8471378461SGreg Roach { 859e18e23bSGreg Roach $router_container = app(RouterContainer::class); 869e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 879e18e23bSGreg Roach 8871378461SGreg Roach $router_container->getMap() 8971378461SGreg Roach ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class) 9071378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST) 9171378461SGreg Roach ->tokens([ 9271378461SGreg Roach 'generations' => '\d+', 9371378461SGreg Roach 'style' => implode('|', array_keys($this->styles())), 9471378461SGreg Roach ]); 9571378461SGreg Roach } 9671378461SGreg Roach 9771378461SGreg Roach /** 980cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 99168ff6f3Sric2016 * 100168ff6f3Sric2016 * @return string 101168ff6f3Sric2016 */ 10249a243cbSGreg Roach public function title(): string 103c1010edaSGreg Roach { 104bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 105bbb76c12SGreg Roach return I18N::translate('Descendants'); 106168ff6f3Sric2016 } 107168ff6f3Sric2016 108168ff6f3Sric2016 /** 109168ff6f3Sric2016 * A sentence describing what this module does. 110168ff6f3Sric2016 * 111168ff6f3Sric2016 * @return string 112168ff6f3Sric2016 */ 11349a243cbSGreg Roach public function description(): string 114c1010edaSGreg Roach { 115bbb76c12SGreg Roach /* I18N: Description of the “DescendancyChart” module */ 116bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s descendants.'); 117168ff6f3Sric2016 } 118168ff6f3Sric2016 119168ff6f3Sric2016 /** 120377a2979SGreg Roach * CSS class for the URL. 121377a2979SGreg Roach * 122377a2979SGreg Roach * @return string 123377a2979SGreg Roach */ 124377a2979SGreg Roach public function chartMenuClass(): string 125377a2979SGreg Roach { 126377a2979SGreg Roach return 'menu-chart-descendants'; 127377a2979SGreg Roach } 128377a2979SGreg Roach 129377a2979SGreg Roach /** 1304eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1314eb71cfaSGreg Roach * 13260bc3e3fSGreg Roach * @param Individual $individual 13360bc3e3fSGreg Roach * 1344eb71cfaSGreg Roach * @return Menu|null 1354eb71cfaSGreg Roach */ 136377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 137c1010edaSGreg Roach { 138e6562982SGreg Roach return $this->chartMenu($individual); 139e6562982SGreg Roach } 140e6562982SGreg Roach 141e6562982SGreg Roach /** 142e6562982SGreg Roach * The title for a specific instance of this chart. 143e6562982SGreg Roach * 144e6562982SGreg Roach * @param Individual $individual 145e6562982SGreg Roach * 146e6562982SGreg Roach * @return string 147e6562982SGreg Roach */ 148e6562982SGreg Roach public function chartTitle(Individual $individual): string 149e6562982SGreg Roach { 150e6562982SGreg Roach /* I18N: %s is an individual’s name */ 15139ca88baSGreg Roach return I18N::translate('Descendants of %s', $individual->fullName()); 152e6562982SGreg Roach } 153e6562982SGreg Roach 154e6562982SGreg Roach /** 15571378461SGreg Roach * The URL for a page showing chart options. 15654452b04SGreg Roach * 15771378461SGreg Roach * @param Individual $individual 15859597b37SGreg Roach * @param mixed[] $parameters 15971378461SGreg Roach * 16071378461SGreg Roach * @return string 16171378461SGreg Roach */ 16271378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 16371378461SGreg Roach { 16471378461SGreg Roach return route(self::ROUTE_NAME, [ 16571378461SGreg Roach 'tree' => $individual->tree()->name(), 16671378461SGreg Roach 'xref' => $individual->xref(), 16771378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 16871378461SGreg Roach } 16971378461SGreg Roach 17071378461SGreg Roach /** 1716ccdf4f0SGreg Roach * @param ServerRequestInterface $request 17254452b04SGreg Roach * 1736ccdf4f0SGreg Roach * @return ResponseInterface 17454452b04SGreg Roach */ 17571378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 17654452b04SGreg Roach { 17757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 17857ab2231SGreg Roach $user = $request->getAttribute('user'); 17971378461SGreg Roach $xref = $request->getAttribute('xref'); 18071378461SGreg Roach $style = $request->getAttribute('style'); 18171378461SGreg Roach $generations = (int) $request->getAttribute('generations'); 1820b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 18354452b04SGreg Roach $individual = Individual::getInstance($xref, $tree); 18454452b04SGreg Roach 18571378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 18671378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 18771378461SGreg Roach return redirect(route(self::ROUTE_NAME, [ 18871378461SGreg Roach 'tree' => $request->getAttribute('tree')->name(), 18971378461SGreg Roach 'xref' => $request->getParsedBody()['xref'], 19071378461SGreg Roach 'style' => $request->getParsedBody()['style'], 19171378461SGreg Roach 'generations' => $request->getParsedBody()['generations'], 19271378461SGreg Roach ])); 19371378461SGreg Roach } 19471378461SGreg Roach 19554452b04SGreg Roach Auth::checkIndividualAccess($individual); 1969867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 19754452b04SGreg Roach 198e759aebbSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 199e759aebbSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 20054452b04SGreg Roach 2010b93976aSGreg Roach if ($ajax === '1') { 20271378461SGreg Roach $this->layout = 'layouts/ajax'; 20371378461SGreg Roach 20471378461SGreg Roach switch ($style) { 20571378461SGreg Roach case self::CHART_STYLE_TREE: 20671378461SGreg Roach return $this->viewResponse('modules/descendancy_chart/tree', [ 20771378461SGreg Roach 'individual' => $individual, 20871378461SGreg Roach 'generations' => $generations, 20971378461SGreg Roach 'daboville' => '1', 21071378461SGreg Roach ]); 21171378461SGreg Roach 21271378461SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 21371378461SGreg Roach return $this->viewResponse('lists/individuals-table', [ 21471378461SGreg Roach 'individuals' => $this->chart_service->descendants($individual, $generations - 1), 21571378461SGreg Roach 'sosa' => false, 21671378461SGreg Roach 'tree' => $tree, 21771378461SGreg Roach ]); 21871378461SGreg Roach 21971378461SGreg Roach case self::CHART_STYLE_FAMILIES: 22071378461SGreg Roach $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 22171378461SGreg Roach 222*ef5d23f1SGreg Roach return $this->viewResponse('lists/families-table', ['families' => $families, 'tree' => $tree]); 22371378461SGreg Roach } 22454452b04SGreg Roach } 22554452b04SGreg Roach 22654452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 22754452b04SGreg Roach 'generations' => $generations, 22871378461SGreg Roach 'style' => $style, 2299b5537c3SGreg Roach 'ajax' => true, 23054452b04SGreg Roach ]); 23154452b04SGreg Roach 2329b5537c3SGreg Roach return $this->viewResponse('modules/descendancy_chart/page', [ 23354452b04SGreg Roach 'ajax_url' => $ajax_url, 23471378461SGreg Roach 'style' => $style, 23571378461SGreg Roach 'styles' => $this->styles(), 236e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 23754452b04SGreg Roach 'generations' => $generations, 23854452b04SGreg Roach 'individual' => $individual, 239e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 240e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 24171378461SGreg Roach 'module' => $this->name(), 24254452b04SGreg Roach 'title' => $this->chartTitle($individual), 243*ef5d23f1SGreg Roach 'tree' => $tree, 24454452b04SGreg Roach ]); 24554452b04SGreg Roach } 24654452b04SGreg Roach 24754452b04SGreg Roach /** 24854452b04SGreg Roach * This chart can display its output in a number of styles 24954452b04SGreg Roach * 2504db4b4a9SGreg Roach * @return string[] 25154452b04SGreg Roach */ 25271378461SGreg Roach protected function styles(): array 25354452b04SGreg Roach { 25454452b04SGreg Roach return [ 2550f1e0f10SGreg Roach self::CHART_STYLE_TREE => I18N::translate('Tree'), 25654452b04SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 25754452b04SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 25854452b04SGreg Roach ]; 259e6562982SGreg Roach } 260168ff6f3Sric2016} 261