1168ff6f3Sric2016<?php 2*3976b470SGreg 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 21e539f5c6SGreg Roachuse Fisharebest\Webtrees\Auth; 22168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 23168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 24e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 25e539f5c6SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 26e539f5c6SGreg Roachuse Fisharebest\Webtrees\Tree; 27e539f5c6SGreg Roachuse Illuminate\Support\Collection; 286ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 30*3976b470SGreg Roach 31f4ba05e3SGreg Roachuse function view; 32168ff6f3Sric2016 33168ff6f3Sric2016/** 34168ff6f3Sric2016 * Class AncestorsChartModule 35168ff6f3Sric2016 */ 3637eb8894SGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleChartInterface 37c1010edaSGreg Roach{ 3849a243cbSGreg Roach use ModuleChartTrait; 3949a243cbSGreg Roach 40e539f5c6SGreg Roach // Chart styles 410f1e0f10SGreg Roach protected const CHART_STYLE_TREE = 'tree'; 42e539f5c6SGreg Roach protected const CHART_STYLE_INDIVIDUALS = 'individuals'; 43e539f5c6SGreg Roach protected const CHART_STYLE_FAMILIES = 'families'; 44e539f5c6SGreg Roach 45e539f5c6SGreg Roach // Defaults 460f1e0f10SGreg Roach protected const DEFAULT_STYLE = self::CHART_STYLE_TREE; 470cf94a7cSGreg Roach protected const DEFAULT_GENERATIONS = '4'; 48e759aebbSGreg Roach 49e759aebbSGreg Roach // Limits 50e759aebbSGreg Roach protected const MINIMUM_GENERATIONS = 2; 51e759aebbSGreg Roach protected const MAXIMUM_GENERATIONS = 10; 52e539f5c6SGreg Roach 5357ab2231SGreg Roach /** @var ChartService */ 5457ab2231SGreg Roach private $chart_service; 5557ab2231SGreg Roach 5657ab2231SGreg Roach /** 5757ab2231SGreg Roach * CompactTreeChartModule constructor. 5857ab2231SGreg Roach * 5957ab2231SGreg Roach * @param ChartService $chart_service 6057ab2231SGreg Roach */ 61*3976b470SGreg Roach public function __construct(ChartService $chart_service) 62*3976b470SGreg Roach { 6357ab2231SGreg Roach $this->chart_service = $chart_service; 6457ab2231SGreg Roach } 6557ab2231SGreg Roach 66168ff6f3Sric2016 /** 670cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 68168ff6f3Sric2016 * 69168ff6f3Sric2016 * @return string 70168ff6f3Sric2016 */ 7149a243cbSGreg Roach public function title(): string 72c1010edaSGreg Roach { 73bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 74bbb76c12SGreg Roach return I18N::translate('Ancestors'); 75168ff6f3Sric2016 } 76168ff6f3Sric2016 77168ff6f3Sric2016 /** 78168ff6f3Sric2016 * A sentence describing what this module does. 79168ff6f3Sric2016 * 80168ff6f3Sric2016 * @return string 81168ff6f3Sric2016 */ 8249a243cbSGreg Roach public function description(): string 83c1010edaSGreg Roach { 84bbb76c12SGreg Roach /* I18N: Description of the “AncestorsChart” module */ 85bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors.'); 86168ff6f3Sric2016 } 87168ff6f3Sric2016 88168ff6f3Sric2016 /** 89377a2979SGreg Roach * CSS class for the URL. 90377a2979SGreg Roach * 91377a2979SGreg Roach * @return string 92377a2979SGreg Roach */ 93377a2979SGreg Roach public function chartMenuClass(): string 94377a2979SGreg Roach { 95377a2979SGreg Roach return 'menu-chart-ancestry'; 96377a2979SGreg Roach } 97377a2979SGreg Roach 98377a2979SGreg Roach /** 994eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 1004eb71cfaSGreg Roach * 10160bc3e3fSGreg Roach * @param Individual $individual 10260bc3e3fSGreg Roach * 1034eb71cfaSGreg Roach * @return Menu|null 1044eb71cfaSGreg Roach */ 105377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 106c1010edaSGreg Roach { 107e6562982SGreg Roach return $this->chartMenu($individual); 108e6562982SGreg Roach } 109e6562982SGreg Roach 110e6562982SGreg Roach /** 111e6562982SGreg Roach * The title for a specific instance of this chart. 112e6562982SGreg Roach * 113e6562982SGreg Roach * @param Individual $individual 114e6562982SGreg Roach * 115e6562982SGreg Roach * @return string 116e6562982SGreg Roach */ 117e6562982SGreg Roach public function chartTitle(Individual $individual): string 118e6562982SGreg Roach { 119e6562982SGreg Roach /* I18N: %s is an individual’s name */ 12039ca88baSGreg Roach return I18N::translate('Ancestors of %s', $individual->fullName()); 121e6562982SGreg Roach } 122e6562982SGreg Roach 123e6562982SGreg Roach /** 124e539f5c6SGreg Roach * A form to request the chart parameters. 125e539f5c6SGreg Roach * 1266ccdf4f0SGreg Roach * @param ServerRequestInterface $request 127e539f5c6SGreg Roach * 1286ccdf4f0SGreg Roach * @return ResponseInterface 129e539f5c6SGreg Roach */ 13057ab2231SGreg Roach public function getChartAction(ServerRequestInterface $request): ResponseInterface 131e539f5c6SGreg Roach { 13257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 13357ab2231SGreg Roach $user = $request->getAttribute('user'); 1340b93976aSGreg Roach $ajax = $request->getQueryParams()['ajax'] ?? ''; 1350b93976aSGreg Roach $xref = $request->getQueryParams()['xref'] ?? ''; 136e539f5c6SGreg Roach $individual = Individual::getInstance($xref, $tree); 137e539f5c6SGreg Roach 138e539f5c6SGreg Roach Auth::checkIndividualAccess($individual); 1399867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 140e539f5c6SGreg Roach 1410b93976aSGreg Roach $chart_style = $request->getQueryParams()['chart_style'] ?? self::DEFAULT_STYLE; 1420b93976aSGreg Roach $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS); 143e539f5c6SGreg Roach 1440cf94a7cSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 1450cf94a7cSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 146e539f5c6SGreg Roach 1470b93976aSGreg Roach if ($ajax === '1') { 14857ab2231SGreg Roach $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations); 149e539f5c6SGreg Roach 150e539f5c6SGreg Roach switch ($chart_style) { 151e539f5c6SGreg Roach default: 1520f1e0f10SGreg Roach case self::CHART_STYLE_TREE: 1530f1e0f10SGreg Roach return response(view('modules/ancestors-chart/tree', ['individual' => $individual, 'parents' => $individual->primaryChildFamily(), 'generations' => $generations, 'sosa' => 1])); 154e539f5c6SGreg Roach 155e539f5c6SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 156e539f5c6SGreg Roach return $this->ancestorsIndividuals($tree, $ancestors); 157e539f5c6SGreg Roach 158e539f5c6SGreg Roach case self::CHART_STYLE_FAMILIES: 159e539f5c6SGreg Roach return $this->ancestorsFamilies($tree, $ancestors); 160e539f5c6SGreg Roach } 161e539f5c6SGreg Roach } 162e539f5c6SGreg Roach 16354452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 16454452b04SGreg Roach 'generations' => $generations, 16554452b04SGreg Roach 'chart_style' => $chart_style, 1669b5537c3SGreg Roach 'ajax' => true, 16754452b04SGreg Roach ]); 16854452b04SGreg Roach 1699b5537c3SGreg Roach return $this->viewResponse('modules/ancestors-chart/page', [ 17054452b04SGreg Roach 'ajax_url' => $ajax_url, 171e539f5c6SGreg Roach 'chart_style' => $chart_style, 172e539f5c6SGreg Roach 'chart_styles' => $this->chartStyles(), 173e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 174e539f5c6SGreg Roach 'generations' => $generations, 175e539f5c6SGreg Roach 'individual' => $individual, 176e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 177e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 17826684e68SGreg Roach 'module_name' => $this->name(), 179e539f5c6SGreg Roach 'title' => $this->chartTitle($individual), 180e539f5c6SGreg Roach ]); 181e539f5c6SGreg Roach } 182e539f5c6SGreg Roach 183e539f5c6SGreg Roach /** 184e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 185e539f5c6SGreg Roach * 186e539f5c6SGreg Roach * @param Tree $tree 187e539f5c6SGreg Roach * @param Collection $ancestors 188e539f5c6SGreg Roach * 1896ccdf4f0SGreg Roach * @return ResponseInterface 190e539f5c6SGreg Roach */ 1916ccdf4f0SGreg Roach protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): ResponseInterface 192e539f5c6SGreg Roach { 193e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 194e539f5c6SGreg Roach 195e539f5c6SGreg Roach return $this->viewResponse('lists/individuals-table', [ 196e539f5c6SGreg Roach 'individuals' => $ancestors, 197e539f5c6SGreg Roach 'sosa' => true, 198e539f5c6SGreg Roach 'tree' => $tree, 199e539f5c6SGreg Roach ]); 200e539f5c6SGreg Roach } 201e539f5c6SGreg Roach 202e539f5c6SGreg Roach /** 203e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 204e539f5c6SGreg Roach * 205e539f5c6SGreg Roach * @param Tree $tree 206e539f5c6SGreg Roach * @param Collection $ancestors 207e539f5c6SGreg Roach * 2086ccdf4f0SGreg Roach * @return ResponseInterface 209e539f5c6SGreg Roach */ 2106ccdf4f0SGreg Roach protected function ancestorsFamilies(Tree $tree, Collection $ancestors): ResponseInterface 211e539f5c6SGreg Roach { 212e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 213e539f5c6SGreg Roach 214e539f5c6SGreg Roach $families = []; 215e539f5c6SGreg Roach foreach ($ancestors as $individual) { 21639ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 217e539f5c6SGreg Roach $families[$family->xref()] = $family; 218e539f5c6SGreg Roach } 219e539f5c6SGreg Roach } 220e539f5c6SGreg Roach 221e539f5c6SGreg Roach return $this->viewResponse('lists/families-table', [ 222e539f5c6SGreg Roach 'families' => $families, 223e539f5c6SGreg Roach 'tree' => $tree, 224e539f5c6SGreg Roach ]); 225e539f5c6SGreg Roach } 226e539f5c6SGreg Roach 227e539f5c6SGreg Roach /** 228e539f5c6SGreg Roach * This chart can display its output in a number of styles 229e539f5c6SGreg Roach * 230e539f5c6SGreg Roach * @return array 231e539f5c6SGreg Roach */ 232e539f5c6SGreg Roach protected function chartStyles(): array 233e539f5c6SGreg Roach { 234e539f5c6SGreg Roach return [ 2350f1e0f10SGreg Roach self::CHART_STYLE_TREE => I18N::translate('Tree'), 236e539f5c6SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 237e539f5c6SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 238e539f5c6SGreg Roach ]; 239e539f5c6SGreg Roach } 240168ff6f3Sric2016} 241