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 20e539f5c6SGreg Roachuse Fisharebest\Webtrees\Auth; 21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 22e539f5c6SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsCharts; 23e539f5c6SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint; 24e539f5c6SGreg Roachuse Fisharebest\Webtrees\Gedcom; 25168ff6f3Sric2016use Fisharebest\Webtrees\I18N; 26168ff6f3Sric2016use Fisharebest\Webtrees\Individual; 27e46b0479SScrutinizer Auto-Fixeruse Fisharebest\Webtrees\Menu; 28e539f5c6SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 29e539f5c6SGreg Roachuse Fisharebest\Webtrees\Tree; 30e539f5c6SGreg Roachuse Illuminate\Support\Collection; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 33168ff6f3Sric2016 34168ff6f3Sric2016/** 35168ff6f3Sric2016 * Class AncestorsChartModule 36168ff6f3Sric2016 */ 3737eb8894SGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleChartInterface 38c1010edaSGreg Roach{ 3949a243cbSGreg Roach use ModuleChartTrait; 4049a243cbSGreg Roach 41e539f5c6SGreg Roach // Chart styles 42e539f5c6SGreg Roach protected const CHART_STYLE_LIST = 'list'; 43e539f5c6SGreg Roach protected const CHART_STYLE_BOOKLET = 'booklet'; 44e539f5c6SGreg Roach protected const CHART_STYLE_INDIVIDUALS = 'individuals'; 45e539f5c6SGreg Roach protected const CHART_STYLE_FAMILIES = 'families'; 46e539f5c6SGreg Roach 47e539f5c6SGreg Roach // Defaults 48e539f5c6SGreg Roach protected const DEFAULT_COUSINS = false; 49e539f5c6SGreg Roach protected const DEFAULT_STYLE = self::CHART_STYLE_LIST; 500cf94a7cSGreg Roach protected const DEFAULT_GENERATIONS = '4'; 51e759aebbSGreg Roach 52e759aebbSGreg Roach // Limits 53e759aebbSGreg Roach protected const MINIMUM_GENERATIONS = 2; 54e759aebbSGreg Roach protected const MAXIMUM_GENERATIONS = 10; 55e539f5c6SGreg Roach 56168ff6f3Sric2016 /** 570cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 58168ff6f3Sric2016 * 59168ff6f3Sric2016 * @return string 60168ff6f3Sric2016 */ 6149a243cbSGreg Roach public function title(): string 62c1010edaSGreg Roach { 63bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 64bbb76c12SGreg Roach return I18N::translate('Ancestors'); 65168ff6f3Sric2016 } 66168ff6f3Sric2016 67168ff6f3Sric2016 /** 68168ff6f3Sric2016 * A sentence describing what this module does. 69168ff6f3Sric2016 * 70168ff6f3Sric2016 * @return string 71168ff6f3Sric2016 */ 7249a243cbSGreg Roach public function description(): string 73c1010edaSGreg Roach { 74bbb76c12SGreg Roach /* I18N: Description of the “AncestorsChart” module */ 75bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors.'); 76168ff6f3Sric2016 } 77168ff6f3Sric2016 78168ff6f3Sric2016 /** 79377a2979SGreg Roach * CSS class for the URL. 80377a2979SGreg Roach * 81377a2979SGreg Roach * @return string 82377a2979SGreg Roach */ 83377a2979SGreg Roach public function chartMenuClass(): string 84377a2979SGreg Roach { 85377a2979SGreg Roach return 'menu-chart-ancestry'; 86377a2979SGreg Roach } 87377a2979SGreg Roach 88377a2979SGreg Roach /** 894eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 904eb71cfaSGreg Roach * 9160bc3e3fSGreg Roach * @param Individual $individual 9260bc3e3fSGreg Roach * 934eb71cfaSGreg Roach * @return Menu|null 944eb71cfaSGreg Roach */ 95377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 96c1010edaSGreg Roach { 97e6562982SGreg Roach return $this->chartMenu($individual); 98e6562982SGreg Roach } 99e6562982SGreg Roach 100e6562982SGreg Roach /** 101e6562982SGreg Roach * The title for a specific instance of this chart. 102e6562982SGreg Roach * 103e6562982SGreg Roach * @param Individual $individual 104e6562982SGreg Roach * 105e6562982SGreg Roach * @return string 106e6562982SGreg Roach */ 107e6562982SGreg Roach public function chartTitle(Individual $individual): string 108e6562982SGreg Roach { 109e6562982SGreg Roach /* I18N: %s is an individual’s name */ 11039ca88baSGreg Roach return I18N::translate('Ancestors of %s', $individual->fullName()); 111e6562982SGreg Roach } 112e6562982SGreg Roach 113e6562982SGreg Roach /** 114e539f5c6SGreg Roach * A form to request the chart parameters. 115e539f5c6SGreg Roach * 1166ccdf4f0SGreg Roach * @param ServerRequestInterface $request 117e539f5c6SGreg Roach * @param Tree $tree 118e5a6b4d4SGreg Roach * @param UserInterface $user 119e539f5c6SGreg Roach * @param ChartService $chart_service 120e539f5c6SGreg Roach * 1216ccdf4f0SGreg Roach * @return ResponseInterface 122e539f5c6SGreg Roach */ 1236ccdf4f0SGreg Roach public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user, ChartService $chart_service): ResponseInterface 124e539f5c6SGreg Roach { 1259b5537c3SGreg Roach $ajax = (bool) $request->get('ajax'); 126e539f5c6SGreg Roach $xref = $request->get('xref', ''); 127e539f5c6SGreg Roach $individual = Individual::getInstance($xref, $tree); 128e539f5c6SGreg Roach 129e539f5c6SGreg Roach Auth::checkIndividualAccess($individual); 1309867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 131e539f5c6SGreg Roach 132e539f5c6SGreg Roach $show_cousins = (bool) $request->get('show_cousins', self::DEFAULT_COUSINS); 133e539f5c6SGreg Roach $chart_style = $request->get('chart_style', self::DEFAULT_STYLE); 134e759aebbSGreg Roach $generations = (int) $request->get('generations', self::DEFAULT_GENERATIONS); 135e539f5c6SGreg Roach 1360cf94a7cSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 1370cf94a7cSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 138e539f5c6SGreg Roach 1399b5537c3SGreg Roach if ($ajax) { 140e539f5c6SGreg Roach $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); 141e539f5c6SGreg Roach 142e539f5c6SGreg Roach switch ($chart_style) { 143e539f5c6SGreg Roach default: 144e539f5c6SGreg Roach case self::CHART_STYLE_LIST: 145*16e8b6e8SGreg Roach return response(view('modules/ancestors-chart/list', ['individual' => $individual, 'parents' => $individual->primaryChildFamily(), 'generations' => $generations, 'sosa' => 1])); 146e539f5c6SGreg Roach 147e539f5c6SGreg Roach case self::CHART_STYLE_BOOKLET: 148e539f5c6SGreg Roach return $this->ancestorsBooklet($ancestors, $show_cousins); 149e539f5c6SGreg Roach 150e539f5c6SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 151e539f5c6SGreg Roach return $this->ancestorsIndividuals($tree, $ancestors); 152e539f5c6SGreg Roach 153e539f5c6SGreg Roach case self::CHART_STYLE_FAMILIES: 154e539f5c6SGreg Roach return $this->ancestorsFamilies($tree, $ancestors); 155e539f5c6SGreg Roach } 156e539f5c6SGreg Roach } 157e539f5c6SGreg Roach 15854452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 15954452b04SGreg Roach 'generations' => $generations, 16054452b04SGreg Roach 'chart_style' => $chart_style, 16154452b04SGreg Roach 'show_cousins' => $show_cousins, 1629b5537c3SGreg Roach 'ajax' => true, 16354452b04SGreg Roach ]); 16454452b04SGreg Roach 1659b5537c3SGreg Roach return $this->viewResponse('modules/ancestors-chart/page', [ 16654452b04SGreg Roach 'ajax_url' => $ajax_url, 167e539f5c6SGreg Roach 'chart_style' => $chart_style, 168e539f5c6SGreg Roach 'chart_styles' => $this->chartStyles(), 169e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 170e539f5c6SGreg Roach 'generations' => $generations, 171e539f5c6SGreg Roach 'individual' => $individual, 172e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 173e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 17426684e68SGreg Roach 'module_name' => $this->name(), 175e539f5c6SGreg Roach 'show_cousins' => $show_cousins, 176e539f5c6SGreg Roach 'title' => $this->chartTitle($individual), 177e539f5c6SGreg Roach ]); 178e539f5c6SGreg Roach } 179e539f5c6SGreg Roach 180e539f5c6SGreg Roach /** 181e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 182e539f5c6SGreg Roach * 183e539f5c6SGreg Roach * @param Tree $tree 184e539f5c6SGreg Roach * @param Collection $ancestors 185e539f5c6SGreg Roach * 1866ccdf4f0SGreg Roach * @return ResponseInterface 187e539f5c6SGreg Roach */ 1886ccdf4f0SGreg Roach protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): ResponseInterface 189e539f5c6SGreg Roach { 190e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 191e539f5c6SGreg Roach 192e539f5c6SGreg Roach return $this->viewResponse('lists/individuals-table', [ 193e539f5c6SGreg Roach 'individuals' => $ancestors, 194e539f5c6SGreg Roach 'sosa' => true, 195e539f5c6SGreg Roach 'tree' => $tree, 196e539f5c6SGreg Roach ]); 197e539f5c6SGreg Roach } 198e539f5c6SGreg Roach 199e539f5c6SGreg Roach /** 200e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 201e539f5c6SGreg Roach * 202e539f5c6SGreg Roach * @param Tree $tree 203e539f5c6SGreg Roach * @param Collection $ancestors 204e539f5c6SGreg Roach * 2056ccdf4f0SGreg Roach * @return ResponseInterface 206e539f5c6SGreg Roach */ 2076ccdf4f0SGreg Roach protected function ancestorsFamilies(Tree $tree, Collection $ancestors): ResponseInterface 208e539f5c6SGreg Roach { 209e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 210e539f5c6SGreg Roach 211e539f5c6SGreg Roach $families = []; 212e539f5c6SGreg Roach foreach ($ancestors as $individual) { 21339ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 214e539f5c6SGreg Roach $families[$family->xref()] = $family; 215e539f5c6SGreg Roach } 216e539f5c6SGreg Roach } 217e539f5c6SGreg Roach 218e539f5c6SGreg Roach return $this->viewResponse('lists/families-table', [ 219e539f5c6SGreg Roach 'families' => $families, 220e539f5c6SGreg Roach 'tree' => $tree, 221e539f5c6SGreg Roach ]); 222e539f5c6SGreg Roach } 223e539f5c6SGreg Roach 224e539f5c6SGreg Roach /** 225e539f5c6SGreg Roach * Show a booklet view of ancestors 226e539f5c6SGreg Roach * 227e539f5c6SGreg Roach * @TODO replace ob_start() with views. 228e539f5c6SGreg Roach * 229e539f5c6SGreg Roach * @param Collection $ancestors 230e539f5c6SGreg Roach * @param bool $show_cousins 231e539f5c6SGreg Roach * 2326ccdf4f0SGreg Roach * @return ResponseInterface 233e539f5c6SGreg Roach */ 2346ccdf4f0SGreg Roach protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): ResponseInterface 235e539f5c6SGreg Roach { 236e539f5c6SGreg Roach ob_start(); 237e539f5c6SGreg Roach 238e539f5c6SGreg Roach echo FunctionsPrint::printPedigreePerson($ancestors[1]); 239e539f5c6SGreg Roach foreach ($ancestors as $sosa => $individual) { 24039ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 241e539f5c6SGreg Roach FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins); 242e539f5c6SGreg Roach } 243e539f5c6SGreg Roach } 244e539f5c6SGreg Roach 245e539f5c6SGreg Roach $html = ob_get_clean(); 246e539f5c6SGreg Roach 2476ccdf4f0SGreg Roach return response($html); 248e539f5c6SGreg Roach } 249e539f5c6SGreg Roach 250e539f5c6SGreg Roach /** 251e539f5c6SGreg Roach * This chart can display its output in a number of styles 252e539f5c6SGreg Roach * 253e539f5c6SGreg Roach * @return array 254e539f5c6SGreg Roach */ 255e539f5c6SGreg Roach protected function chartStyles(): array 256e539f5c6SGreg Roach { 257e539f5c6SGreg Roach return [ 258e539f5c6SGreg Roach self::CHART_STYLE_LIST => I18N::translate('List'), 259e539f5c6SGreg Roach self::CHART_STYLE_BOOKLET => I18N::translate('Booklet'), 260e539f5c6SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 261e539f5c6SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 262e539f5c6SGreg Roach ]; 263e539f5c6SGreg Roach } 264168ff6f3Sric2016} 265