1168ff6f3Sric2016<?php 2168ff6f3Sric2016/** 3168ff6f3Sric2016 * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5168ff6f3Sric2016 * This program is free software: you can redistribute it and/or modify 6168ff6f3Sric2016 * it under the terms of the GNU General Public License as published by 7168ff6f3Sric2016 * the Free Software Foundation, either version 3 of the License, or 8168ff6f3Sric2016 * (at your option) any later version. 9168ff6f3Sric2016 * This program is distributed in the hope that it will be useful, 10168ff6f3Sric2016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11168ff6f3Sric2016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12168ff6f3Sric2016 * GNU General Public License for more details. 13168ff6f3Sric2016 * You should have received a copy of the GNU General Public License 14168ff6f3Sric2016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15168ff6f3Sric2016 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 18168ff6f3Sric2016namespace Fisharebest\Webtrees\Module; 19168ff6f3Sric2016 2054452b04SGreg Roachuse Fisharebest\Webtrees\Auth; 21168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 22168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 23e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 2454452b04SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 2554452b04SGreg Roachuse Fisharebest\Webtrees\Tree; 2654452b04SGreg Roachuse Illuminate\Support\Collection; 276ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 29168ff6f3Sric2016 30168ff6f3Sric2016/** 31168ff6f3Sric2016 * Class DescendancyChartModule 32168ff6f3Sric2016 */ 3337eb8894SGreg Roachclass DescendancyChartModule extends AbstractModule implements ModuleChartInterface 34c1010edaSGreg Roach{ 3549a243cbSGreg Roach use ModuleChartTrait; 3649a243cbSGreg Roach 3754452b04SGreg Roach // Chart styles 3831e26437SGreg Roach public const CHART_STYLE_TREE = 'tree'; 3931e26437SGreg Roach public const CHART_STYLE_INDIVIDUALS = 'individuals'; 4031e26437SGreg Roach public const CHART_STYLE_FAMILIES = 'families'; 4154452b04SGreg Roach 4254452b04SGreg Roach // Defaults 430f1e0f10SGreg Roach public const DEFAULT_STYLE = self::CHART_STYLE_TREE; 44677aaceaSGreg Roach public const DEFAULT_GENERATIONS = '3'; 45e759aebbSGreg Roach 46e759aebbSGreg Roach // Limits 47e759aebbSGreg Roach public const MINIMUM_GENERATIONS = 2; 48e759aebbSGreg Roach public const MAXIMUM_GENERATIONS = 10; 4954452b04SGreg Roach 5054452b04SGreg Roach /** @var int[] */ 5154452b04SGreg Roach protected $dabo_num = []; 5254452b04SGreg Roach 5354452b04SGreg Roach /** @var string[] */ 5454452b04SGreg Roach protected $dabo_sex = []; 5554452b04SGreg Roach 56*57ab2231SGreg Roach /** @var ChartService */ 57*57ab2231SGreg Roach private $chart_service; 58*57ab2231SGreg Roach 59*57ab2231SGreg Roach /** 60*57ab2231SGreg Roach * DescendancyChartModule constructor. 61*57ab2231SGreg Roach * 62*57ab2231SGreg Roach * @param ChartService $chart_service 63*57ab2231SGreg Roach */ 64*57ab2231SGreg Roach public function __construct(ChartService $chart_service) { 65*57ab2231SGreg Roach $this->chart_service = $chart_service; 66*57ab2231SGreg Roach } 67*57ab2231SGreg Roach 68168ff6f3Sric2016 /** 690cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 70168ff6f3Sric2016 * 71168ff6f3Sric2016 * @return string 72168ff6f3Sric2016 */ 7349a243cbSGreg Roach public function title(): string 74c1010edaSGreg Roach { 75bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 76bbb76c12SGreg Roach return I18N::translate('Descendants'); 77168ff6f3Sric2016 } 78168ff6f3Sric2016 79168ff6f3Sric2016 /** 80168ff6f3Sric2016 * A sentence describing what this module does. 81168ff6f3Sric2016 * 82168ff6f3Sric2016 * @return string 83168ff6f3Sric2016 */ 8449a243cbSGreg Roach public function description(): string 85c1010edaSGreg Roach { 86bbb76c12SGreg Roach /* I18N: Description of the “DescendancyChart” module */ 87bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s descendants.'); 88168ff6f3Sric2016 } 89168ff6f3Sric2016 90168ff6f3Sric2016 /** 91377a2979SGreg Roach * CSS class for the URL. 92377a2979SGreg Roach * 93377a2979SGreg Roach * @return string 94377a2979SGreg Roach */ 95377a2979SGreg Roach public function chartMenuClass(): string 96377a2979SGreg Roach { 97377a2979SGreg Roach return 'menu-chart-descendants'; 98377a2979SGreg Roach } 99377a2979SGreg Roach 100377a2979SGreg Roach /** 1014eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1024eb71cfaSGreg Roach * 10360bc3e3fSGreg Roach * @param Individual $individual 10460bc3e3fSGreg Roach * 1054eb71cfaSGreg Roach * @return Menu|null 1064eb71cfaSGreg Roach */ 107377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 108c1010edaSGreg Roach { 109e6562982SGreg Roach return $this->chartMenu($individual); 110e6562982SGreg Roach } 111e6562982SGreg Roach 112e6562982SGreg Roach /** 113e6562982SGreg Roach * The title for a specific instance of this chart. 114e6562982SGreg Roach * 115e6562982SGreg Roach * @param Individual $individual 116e6562982SGreg Roach * 117e6562982SGreg Roach * @return string 118e6562982SGreg Roach */ 119e6562982SGreg Roach public function chartTitle(Individual $individual): string 120e6562982SGreg Roach { 121e6562982SGreg Roach /* I18N: %s is an individual’s name */ 12239ca88baSGreg Roach return I18N::translate('Descendants of %s', $individual->fullName()); 123e6562982SGreg Roach } 124e6562982SGreg Roach 125e6562982SGreg Roach /** 12654452b04SGreg Roach * A form to request the chart parameters. 12754452b04SGreg Roach * 1286ccdf4f0SGreg Roach * @param ServerRequestInterface $request 12954452b04SGreg Roach * 1306ccdf4f0SGreg Roach * @return ResponseInterface 13154452b04SGreg Roach */ 132*57ab2231SGreg Roach public function getChartAction(ServerRequestInterface $request): ResponseInterface 13354452b04SGreg Roach { 134*57ab2231SGreg Roach $tree = $request->getAttribute('tree'); 135*57ab2231SGreg Roach $user = $request->getAttribute('user'); 1360b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 13716e8b6e8SGreg Roach $xref = $request->getQueryParams()['xref'] ?? ''; 13854452b04SGreg Roach $individual = Individual::getInstance($xref, $tree); 13954452b04SGreg Roach 14054452b04SGreg Roach Auth::checkIndividualAccess($individual); 1419867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 14254452b04SGreg Roach 14331e26437SGreg Roach $chart_style = $request->getQueryParams()['chart_style'] ?? self::DEFAULT_STYLE; 14416e8b6e8SGreg Roach $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS); 14554452b04SGreg Roach 146e759aebbSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 147e759aebbSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 14854452b04SGreg Roach 1490b93976aSGreg Roach if ($ajax === '1') { 150*57ab2231SGreg Roach return $this->chart($request); 15154452b04SGreg Roach } 15254452b04SGreg Roach 15354452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 15454452b04SGreg Roach 'chart_style' => $chart_style, 15554452b04SGreg Roach 'generations' => $generations, 1569b5537c3SGreg Roach 'ajax' => true, 15754452b04SGreg Roach ]); 15854452b04SGreg Roach 1599b5537c3SGreg Roach return $this->viewResponse('modules/descendancy_chart/page', [ 16054452b04SGreg Roach 'ajax_url' => $ajax_url, 16154452b04SGreg Roach 'chart_style' => $chart_style, 16254452b04SGreg Roach 'chart_styles' => $this->chartStyles(), 163e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 16454452b04SGreg Roach 'generations' => $generations, 16554452b04SGreg Roach 'individual' => $individual, 166e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 167e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 16826684e68SGreg Roach 'module_name' => $this->name(), 16954452b04SGreg Roach 'title' => $this->chartTitle($individual), 17054452b04SGreg Roach ]); 17154452b04SGreg Roach } 17254452b04SGreg Roach 17354452b04SGreg Roach /** 1746ccdf4f0SGreg Roach * @param ServerRequestInterface $request 17554452b04SGreg Roach * 1766ccdf4f0SGreg Roach * @return ResponseInterface 17754452b04SGreg Roach */ 178*57ab2231SGreg Roach public function chart(ServerRequestInterface $request): ResponseInterface 17954452b04SGreg Roach { 18054452b04SGreg Roach $this->layout = 'layouts/ajax'; 18154452b04SGreg Roach 182*57ab2231SGreg Roach $tree = $request->getAttribute('tree'); 18316e8b6e8SGreg Roach $xref = $request->getQueryParams()['xref']; 18454452b04SGreg Roach $individual = Individual::getInstance($xref, $tree); 18554452b04SGreg Roach 18654452b04SGreg Roach Auth::checkIndividualAccess($individual); 18754452b04SGreg Roach 18831e26437SGreg Roach $chart_style = $request->getQueryParams()['chart_style']; 18916e8b6e8SGreg Roach $generations = (int) $request->getQueryParams()['generations']; 19054452b04SGreg Roach 191e759aebbSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 192e759aebbSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 19354452b04SGreg Roach 19454452b04SGreg Roach switch ($chart_style) { 1950f1e0f10SGreg Roach case self::CHART_STYLE_TREE: 19654452b04SGreg Roach default: 1970f1e0f10SGreg Roach return response(view('modules/descendancy_chart/tree', ['individual' => $individual, 'generations' => $generations, 'daboville' => '1'])); 19854452b04SGreg Roach 19954452b04SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 200*57ab2231SGreg Roach $individuals = $this->chart_service->descendants($individual, $generations - 1); 20154452b04SGreg Roach 20254452b04SGreg Roach return $this->descendantsIndividuals($tree, $individuals); 20354452b04SGreg Roach 20454452b04SGreg Roach case self::CHART_STYLE_FAMILIES: 205*57ab2231SGreg Roach $families = $this->chart_service->descendantFamilies($individual, $generations - 1); 20654452b04SGreg Roach 20754452b04SGreg Roach return $this->descendantsFamilies($tree, $families); 20854452b04SGreg Roach } 20954452b04SGreg Roach } 21054452b04SGreg Roach 21154452b04SGreg Roach /** 21254452b04SGreg Roach * Show a tabular list of individual descendants. 21354452b04SGreg Roach * 21454452b04SGreg Roach * @param Tree $tree 21554452b04SGreg Roach * @param Collection $individuals 21654452b04SGreg Roach * 2176ccdf4f0SGreg Roach * @return ResponseInterface 21854452b04SGreg Roach */ 2196ccdf4f0SGreg Roach private function descendantsIndividuals(Tree $tree, Collection $individuals): ResponseInterface 22054452b04SGreg Roach { 22154452b04SGreg Roach $this->layout = 'layouts/ajax'; 22254452b04SGreg Roach 22354452b04SGreg Roach return $this->viewResponse('lists/individuals-table', [ 22454452b04SGreg Roach 'individuals' => $individuals, 22554452b04SGreg Roach 'sosa' => false, 22654452b04SGreg Roach 'tree' => $tree, 22754452b04SGreg Roach ]); 22854452b04SGreg Roach } 22954452b04SGreg Roach 23054452b04SGreg Roach /** 23154452b04SGreg Roach * Show a tabular list of individual descendants. 23254452b04SGreg Roach * 23354452b04SGreg Roach * @param Tree $tree 23454452b04SGreg Roach * @param Collection $families 23554452b04SGreg Roach * 2366ccdf4f0SGreg Roach * @return ResponseInterface 23754452b04SGreg Roach */ 2386ccdf4f0SGreg Roach private function descendantsFamilies(Tree $tree, Collection $families): ResponseInterface 23954452b04SGreg Roach { 24054452b04SGreg Roach $this->layout = 'layouts/ajax'; 24154452b04SGreg Roach 24254452b04SGreg Roach return $this->viewResponse('lists/families-table', [ 24354452b04SGreg Roach 'families' => $families, 24454452b04SGreg Roach 'tree' => $tree, 24554452b04SGreg Roach ]); 24654452b04SGreg Roach } 24754452b04SGreg Roach 24854452b04SGreg Roach /** 24954452b04SGreg Roach * This chart can display its output in a number of styles 25054452b04SGreg Roach * 2514db4b4a9SGreg Roach * @return string[] 25254452b04SGreg Roach */ 25354452b04SGreg Roach private function chartStyles(): array 25454452b04SGreg Roach { 25554452b04SGreg Roach return [ 2560f1e0f10SGreg Roach self::CHART_STYLE_TREE => I18N::translate('Tree'), 25754452b04SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 25854452b04SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 25954452b04SGreg Roach ]; 260e6562982SGreg Roach } 261168ff6f3Sric2016} 262