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 */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 19168ff6f3Sric2016namespace Fisharebest\Webtrees\Module; 20168ff6f3Sric2016 21*71378461SGreg Roachuse Aura\Router\RouterContainer; 22*71378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 2354452b04SGreg Roachuse Fisharebest\Webtrees\Auth; 24168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 25168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 26e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 2754452b04SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 30*71378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 31*71378461SGreg Roach 32*71378461SGreg Roachuse function max; 33*71378461SGreg Roachuse function min; 34*71378461SGreg Roachuse function route; 35168ff6f3Sric2016 36168ff6f3Sric2016/** 37168ff6f3Sric2016 * Class DescendancyChartModule 38168ff6f3Sric2016 */ 39*71378461SGreg Roachclass DescendancyChartModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 40c1010edaSGreg Roach{ 4149a243cbSGreg Roach use ModuleChartTrait; 4249a243cbSGreg Roach 43*71378461SGreg Roach private const ROUTE_NAME = 'descendancy-chart'; 44*71378461SGreg Roach private const ROUTE_URL = '/tree/{tree}/descendants-{style}-{generations}/{xref}'; 45*71378461SGreg Roach 4654452b04SGreg Roach // Chart styles 4731e26437SGreg Roach public const CHART_STYLE_TREE = 'tree'; 4831e26437SGreg Roach public const CHART_STYLE_INDIVIDUALS = 'individuals'; 4931e26437SGreg Roach public const CHART_STYLE_FAMILIES = 'families'; 5054452b04SGreg Roach 5154452b04SGreg Roach // Defaults 520f1e0f10SGreg Roach public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 53677aaceaSGreg Roach public const DEFAULT_GENERATIONS = '3'; 54*71378461SGreg Roach protected const DEFAULT_PARAMETERS = [ 55*71378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 56*71378461SGreg Roach 'style' => self::DEFAULT_STYLE, 57*71378461SGreg Roach ]; 58e759aebbSGreg Roach 59e759aebbSGreg Roach // Limits 60*71378461SGreg Roach protected const MINIMUM_GENERATIONS = 2; 61*71378461SGreg Roach protected const MAXIMUM_GENERATIONS = 10; 6254452b04SGreg Roach 6357ab2231SGreg Roach /** @var ChartService */ 6457ab2231SGreg Roach private $chart_service; 6557ab2231SGreg Roach 6657ab2231SGreg Roach /** 6757ab2231SGreg Roach * DescendancyChartModule constructor. 6857ab2231SGreg Roach * 6957ab2231SGreg Roach * @param ChartService $chart_service 7057ab2231SGreg Roach */ 713976b470SGreg Roach public function __construct(ChartService $chart_service) 723976b470SGreg Roach { 7357ab2231SGreg Roach $this->chart_service = $chart_service; 7457ab2231SGreg Roach } 7557ab2231SGreg Roach 76168ff6f3Sric2016 /** 77*71378461SGreg Roach * Initialization. 78*71378461SGreg Roach * 79*71378461SGreg Roach * @param RouterContainer $router_container 80*71378461SGreg Roach */ 81*71378461SGreg Roach public function boot(RouterContainer $router_container) 82*71378461SGreg Roach { 83*71378461SGreg Roach $router_container->getMap() 84*71378461SGreg Roach ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class) 85*71378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST) 86*71378461SGreg Roach ->tokens([ 87*71378461SGreg Roach 'generations' => '\d+', 88*71378461SGreg Roach 'style' => implode('|', array_keys($this->styles())), 89*71378461SGreg Roach ]); 90*71378461SGreg Roach } 91*71378461SGreg Roach 92*71378461SGreg Roach /** 930cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 94168ff6f3Sric2016 * 95168ff6f3Sric2016 * @return string 96168ff6f3Sric2016 */ 9749a243cbSGreg Roach public function title(): string 98c1010edaSGreg Roach { 99bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 100bbb76c12SGreg Roach return I18N::translate('Descendants'); 101168ff6f3Sric2016 } 102168ff6f3Sric2016 103168ff6f3Sric2016 /** 104168ff6f3Sric2016 * A sentence describing what this module does. 105168ff6f3Sric2016 * 106168ff6f3Sric2016 * @return string 107168ff6f3Sric2016 */ 10849a243cbSGreg Roach public function description(): string 109c1010edaSGreg Roach { 110bbb76c12SGreg Roach /* I18N: Description of the “DescendancyChart” module */ 111bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s descendants.'); 112168ff6f3Sric2016 } 113168ff6f3Sric2016 114168ff6f3Sric2016 /** 115377a2979SGreg Roach * CSS class for the URL. 116377a2979SGreg Roach * 117377a2979SGreg Roach * @return string 118377a2979SGreg Roach */ 119377a2979SGreg Roach public function chartMenuClass(): string 120377a2979SGreg Roach { 121377a2979SGreg Roach return 'menu-chart-descendants'; 122377a2979SGreg Roach } 123377a2979SGreg Roach 124377a2979SGreg Roach /** 1254eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1264eb71cfaSGreg Roach * 12760bc3e3fSGreg Roach * @param Individual $individual 12860bc3e3fSGreg Roach * 1294eb71cfaSGreg Roach * @return Menu|null 1304eb71cfaSGreg Roach */ 131377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 132c1010edaSGreg Roach { 133e6562982SGreg Roach return $this->chartMenu($individual); 134e6562982SGreg Roach } 135e6562982SGreg Roach 136e6562982SGreg Roach /** 137e6562982SGreg Roach * The title for a specific instance of this chart. 138e6562982SGreg Roach * 139e6562982SGreg Roach * @param Individual $individual 140e6562982SGreg Roach * 141e6562982SGreg Roach * @return string 142e6562982SGreg Roach */ 143e6562982SGreg Roach public function chartTitle(Individual $individual): string 144e6562982SGreg Roach { 145e6562982SGreg Roach /* I18N: %s is an individual’s name */ 14639ca88baSGreg Roach return I18N::translate('Descendants of %s', $individual->fullName()); 147e6562982SGreg Roach } 148e6562982SGreg Roach 149e6562982SGreg Roach /** 150*71378461SGreg Roach * The URL for a page showing chart options. 15154452b04SGreg Roach * 152*71378461SGreg Roach * @param Individual $individual 153*71378461SGreg Roach * @param string[] $parameters 154*71378461SGreg Roach * 155*71378461SGreg Roach * @return string 156*71378461SGreg Roach */ 157*71378461SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 158*71378461SGreg Roach { 159*71378461SGreg Roach return route(self::ROUTE_NAME, [ 160*71378461SGreg Roach 'tree' => $individual->tree()->name(), 161*71378461SGreg Roach 'xref' => $individual->xref(), 162*71378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 163*71378461SGreg Roach } 164*71378461SGreg Roach 165*71378461SGreg Roach /** 1666ccdf4f0SGreg Roach * @param ServerRequestInterface $request 16754452b04SGreg Roach * 1686ccdf4f0SGreg Roach * @return ResponseInterface 16954452b04SGreg Roach */ 170*71378461SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 17154452b04SGreg Roach { 17257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 17357ab2231SGreg Roach $user = $request->getAttribute('user'); 174*71378461SGreg Roach $xref = $request->getAttribute('xref'); 175*71378461SGreg Roach $style = $request->getAttribute('style'); 176*71378461SGreg Roach $generations = (int) $request->getAttribute('generations'); 1770b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 17854452b04SGreg Roach $individual = Individual::getInstance($xref, $tree); 17954452b04SGreg Roach 180*71378461SGreg Roach // Convert POST requests into GET requests for pretty URLs. 181*71378461SGreg Roach if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 182*71378461SGreg Roach return redirect(route(self::ROUTE_NAME, [ 183*71378461SGreg Roach 'tree' => $request->getAttribute('tree')->name(), 184*71378461SGreg Roach 'xref' => $request->getParsedBody()['xref'], 185*71378461SGreg Roach 'style' => $request->getParsedBody()['style'], 186*71378461SGreg Roach 'generations' => $request->getParsedBody()['generations'], 187*71378461SGreg Roach ])); 188*71378461SGreg Roach } 189*71378461SGreg Roach 19054452b04SGreg Roach Auth::checkIndividualAccess($individual); 1919867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 19254452b04SGreg Roach 193e759aebbSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 194e759aebbSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 19554452b04SGreg Roach 1960b93976aSGreg Roach if ($ajax === '1') { 197*71378461SGreg Roach $this->layout = 'layouts/ajax'; 198*71378461SGreg Roach 199*71378461SGreg Roach switch ($style) { 200*71378461SGreg Roach case self::CHART_STYLE_TREE: 201*71378461SGreg Roach return $this->viewResponse('modules/descendancy_chart/tree', [ 202*71378461SGreg Roach 'individual' => $individual, 203*71378461SGreg Roach 'generations' => $generations, 204*71378461SGreg Roach 'daboville' => '1', 205*71378461SGreg Roach ]); 206*71378461SGreg Roach 207*71378461SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 208*71378461SGreg Roach return $this->viewResponse('lists/individuals-table', [ 209*71378461SGreg Roach 'individuals' => $this->chart_service->descendants($individual, $generations - 1), 210*71378461SGreg Roach 'sosa' => false, 211*71378461SGreg Roach 'tree' => $tree, 212*71378461SGreg Roach ]); 213*71378461SGreg Roach 214*71378461SGreg Roach case self::CHART_STYLE_FAMILIES: 215*71378461SGreg Roach $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 216*71378461SGreg Roach 217*71378461SGreg Roach return $this->viewResponse('lists/families-table', [ 218*71378461SGreg Roach 'families' => $families, 219*71378461SGreg Roach 'tree' => $tree, 220*71378461SGreg Roach ]); 221*71378461SGreg Roach } 22254452b04SGreg Roach } 22354452b04SGreg Roach 22454452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 22554452b04SGreg Roach 'generations' => $generations, 226*71378461SGreg Roach 'style' => $style, 2279b5537c3SGreg Roach 'ajax' => true, 22854452b04SGreg Roach ]); 22954452b04SGreg Roach 2309b5537c3SGreg Roach return $this->viewResponse('modules/descendancy_chart/page', [ 23154452b04SGreg Roach 'ajax_url' => $ajax_url, 232*71378461SGreg Roach 'style' => $style, 233*71378461SGreg Roach 'styles' => $this->styles(), 234e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 23554452b04SGreg Roach 'generations' => $generations, 23654452b04SGreg Roach 'individual' => $individual, 237e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 238e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 239*71378461SGreg Roach 'module' => $this->name(), 24054452b04SGreg Roach 'title' => $this->chartTitle($individual), 24154452b04SGreg Roach ]); 24254452b04SGreg Roach } 24354452b04SGreg Roach 24454452b04SGreg Roach /** 24554452b04SGreg Roach * This chart can display its output in a number of styles 24654452b04SGreg Roach * 2474db4b4a9SGreg Roach * @return string[] 24854452b04SGreg Roach */ 249*71378461SGreg 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