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; 33*f4ba05e3SGreg Roachuse function view; 34168ff6f3Sric2016 35168ff6f3Sric2016/** 36168ff6f3Sric2016 * Class AncestorsChartModule 37168ff6f3Sric2016 */ 3837eb8894SGreg Roachclass AncestorsChartModule extends AbstractModule implements ModuleChartInterface 39c1010edaSGreg Roach{ 4049a243cbSGreg Roach use ModuleChartTrait; 4149a243cbSGreg Roach 42e539f5c6SGreg Roach // Chart styles 43e539f5c6SGreg Roach protected const CHART_STYLE_LIST = 'list'; 44e539f5c6SGreg Roach protected const CHART_STYLE_BOOKLET = 'booklet'; 45e539f5c6SGreg Roach protected const CHART_STYLE_INDIVIDUALS = 'individuals'; 46e539f5c6SGreg Roach protected const CHART_STYLE_FAMILIES = 'families'; 47e539f5c6SGreg Roach 48e539f5c6SGreg Roach // Defaults 49e539f5c6SGreg Roach protected const DEFAULT_COUSINS = false; 50e539f5c6SGreg Roach protected const DEFAULT_STYLE = self::CHART_STYLE_LIST; 510cf94a7cSGreg Roach protected const DEFAULT_GENERATIONS = '4'; 52e759aebbSGreg Roach 53e759aebbSGreg Roach // Limits 54e759aebbSGreg Roach protected const MINIMUM_GENERATIONS = 2; 55e759aebbSGreg Roach protected const MAXIMUM_GENERATIONS = 10; 56e539f5c6SGreg Roach 57168ff6f3Sric2016 /** 580cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 59168ff6f3Sric2016 * 60168ff6f3Sric2016 * @return string 61168ff6f3Sric2016 */ 6249a243cbSGreg Roach public function title(): string 63c1010edaSGreg Roach { 64bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 65bbb76c12SGreg Roach return I18N::translate('Ancestors'); 66168ff6f3Sric2016 } 67168ff6f3Sric2016 68168ff6f3Sric2016 /** 69168ff6f3Sric2016 * A sentence describing what this module does. 70168ff6f3Sric2016 * 71168ff6f3Sric2016 * @return string 72168ff6f3Sric2016 */ 7349a243cbSGreg Roach public function description(): string 74c1010edaSGreg Roach { 75bbb76c12SGreg Roach /* I18N: Description of the “AncestorsChart” module */ 76bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors.'); 77168ff6f3Sric2016 } 78168ff6f3Sric2016 79168ff6f3Sric2016 /** 80377a2979SGreg Roach * CSS class for the URL. 81377a2979SGreg Roach * 82377a2979SGreg Roach * @return string 83377a2979SGreg Roach */ 84377a2979SGreg Roach public function chartMenuClass(): string 85377a2979SGreg Roach { 86377a2979SGreg Roach return 'menu-chart-ancestry'; 87377a2979SGreg Roach } 88377a2979SGreg Roach 89377a2979SGreg Roach /** 904eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 914eb71cfaSGreg Roach * 9260bc3e3fSGreg Roach * @param Individual $individual 9360bc3e3fSGreg Roach * 944eb71cfaSGreg Roach * @return Menu|null 954eb71cfaSGreg Roach */ 96377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 97c1010edaSGreg Roach { 98e6562982SGreg Roach return $this->chartMenu($individual); 99e6562982SGreg Roach } 100e6562982SGreg Roach 101e6562982SGreg Roach /** 102e6562982SGreg Roach * The title for a specific instance of this chart. 103e6562982SGreg Roach * 104e6562982SGreg Roach * @param Individual $individual 105e6562982SGreg Roach * 106e6562982SGreg Roach * @return string 107e6562982SGreg Roach */ 108e6562982SGreg Roach public function chartTitle(Individual $individual): string 109e6562982SGreg Roach { 110e6562982SGreg Roach /* I18N: %s is an individual’s name */ 11139ca88baSGreg Roach return I18N::translate('Ancestors of %s', $individual->fullName()); 112e6562982SGreg Roach } 113e6562982SGreg Roach 114e6562982SGreg Roach /** 115e539f5c6SGreg Roach * A form to request the chart parameters. 116e539f5c6SGreg Roach * 1176ccdf4f0SGreg Roach * @param ServerRequestInterface $request 118e539f5c6SGreg Roach * @param Tree $tree 119e5a6b4d4SGreg Roach * @param UserInterface $user 120e539f5c6SGreg Roach * @param ChartService $chart_service 121e539f5c6SGreg Roach * 1226ccdf4f0SGreg Roach * @return ResponseInterface 123e539f5c6SGreg Roach */ 1246ccdf4f0SGreg Roach public function getChartAction(ServerRequestInterface $request, Tree $tree, UserInterface $user, ChartService $chart_service): ResponseInterface 125e539f5c6SGreg Roach { 1269b5537c3SGreg Roach $ajax = (bool) $request->get('ajax'); 127e539f5c6SGreg Roach $xref = $request->get('xref', ''); 128e539f5c6SGreg Roach $individual = Individual::getInstance($xref, $tree); 129e539f5c6SGreg Roach 130e539f5c6SGreg Roach Auth::checkIndividualAccess($individual); 1319867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 132e539f5c6SGreg Roach 133e539f5c6SGreg Roach $show_cousins = (bool) $request->get('show_cousins', self::DEFAULT_COUSINS); 134e539f5c6SGreg Roach $chart_style = $request->get('chart_style', self::DEFAULT_STYLE); 135e759aebbSGreg Roach $generations = (int) $request->get('generations', self::DEFAULT_GENERATIONS); 136e539f5c6SGreg Roach 1370cf94a7cSGreg Roach $generations = min($generations, self::MAXIMUM_GENERATIONS); 1380cf94a7cSGreg Roach $generations = max($generations, self::MINIMUM_GENERATIONS); 139e539f5c6SGreg Roach 1409b5537c3SGreg Roach if ($ajax) { 141e539f5c6SGreg Roach $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); 142e539f5c6SGreg Roach 143e539f5c6SGreg Roach switch ($chart_style) { 144e539f5c6SGreg Roach default: 145e539f5c6SGreg Roach case self::CHART_STYLE_LIST: 14616e8b6e8SGreg Roach return response(view('modules/ancestors-chart/list', ['individual' => $individual, 'parents' => $individual->primaryChildFamily(), 'generations' => $generations, 'sosa' => 1])); 147e539f5c6SGreg Roach 148e539f5c6SGreg Roach case self::CHART_STYLE_BOOKLET: 149e539f5c6SGreg Roach return $this->ancestorsBooklet($ancestors, $show_cousins); 150e539f5c6SGreg Roach 151e539f5c6SGreg Roach case self::CHART_STYLE_INDIVIDUALS: 152e539f5c6SGreg Roach return $this->ancestorsIndividuals($tree, $ancestors); 153e539f5c6SGreg Roach 154e539f5c6SGreg Roach case self::CHART_STYLE_FAMILIES: 155e539f5c6SGreg Roach return $this->ancestorsFamilies($tree, $ancestors); 156e539f5c6SGreg Roach } 157e539f5c6SGreg Roach } 158e539f5c6SGreg Roach 15954452b04SGreg Roach $ajax_url = $this->chartUrl($individual, [ 16054452b04SGreg Roach 'generations' => $generations, 16154452b04SGreg Roach 'chart_style' => $chart_style, 16254452b04SGreg Roach 'show_cousins' => $show_cousins, 1639b5537c3SGreg Roach 'ajax' => true, 16454452b04SGreg Roach ]); 16554452b04SGreg Roach 1669b5537c3SGreg Roach return $this->viewResponse('modules/ancestors-chart/page', [ 16754452b04SGreg Roach 'ajax_url' => $ajax_url, 168e539f5c6SGreg Roach 'chart_style' => $chart_style, 169e539f5c6SGreg Roach 'chart_styles' => $this->chartStyles(), 170e759aebbSGreg Roach 'default_generations' => self::DEFAULT_GENERATIONS, 171e539f5c6SGreg Roach 'generations' => $generations, 172e539f5c6SGreg Roach 'individual' => $individual, 173e759aebbSGreg Roach 'maximum_generations' => self::MAXIMUM_GENERATIONS, 174e759aebbSGreg Roach 'minimum_generations' => self::MINIMUM_GENERATIONS, 17526684e68SGreg Roach 'module_name' => $this->name(), 176e539f5c6SGreg Roach 'show_cousins' => $show_cousins, 177e539f5c6SGreg Roach 'title' => $this->chartTitle($individual), 178e539f5c6SGreg Roach ]); 179e539f5c6SGreg Roach } 180e539f5c6SGreg Roach 181e539f5c6SGreg Roach /** 182e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 183e539f5c6SGreg Roach * 184e539f5c6SGreg Roach * @param Tree $tree 185e539f5c6SGreg Roach * @param Collection $ancestors 186e539f5c6SGreg Roach * 1876ccdf4f0SGreg Roach * @return ResponseInterface 188e539f5c6SGreg Roach */ 1896ccdf4f0SGreg Roach protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): ResponseInterface 190e539f5c6SGreg Roach { 191e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 192e539f5c6SGreg Roach 193e539f5c6SGreg Roach return $this->viewResponse('lists/individuals-table', [ 194e539f5c6SGreg Roach 'individuals' => $ancestors, 195e539f5c6SGreg Roach 'sosa' => true, 196e539f5c6SGreg Roach 'tree' => $tree, 197e539f5c6SGreg Roach ]); 198e539f5c6SGreg Roach } 199e539f5c6SGreg Roach 200e539f5c6SGreg Roach /** 201e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 202e539f5c6SGreg Roach * 203e539f5c6SGreg Roach * @param Tree $tree 204e539f5c6SGreg Roach * @param Collection $ancestors 205e539f5c6SGreg Roach * 2066ccdf4f0SGreg Roach * @return ResponseInterface 207e539f5c6SGreg Roach */ 2086ccdf4f0SGreg Roach protected function ancestorsFamilies(Tree $tree, Collection $ancestors): ResponseInterface 209e539f5c6SGreg Roach { 210e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 211e539f5c6SGreg Roach 212e539f5c6SGreg Roach $families = []; 213e539f5c6SGreg Roach foreach ($ancestors as $individual) { 21439ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 215e539f5c6SGreg Roach $families[$family->xref()] = $family; 216e539f5c6SGreg Roach } 217e539f5c6SGreg Roach } 218e539f5c6SGreg Roach 219e539f5c6SGreg Roach return $this->viewResponse('lists/families-table', [ 220e539f5c6SGreg Roach 'families' => $families, 221e539f5c6SGreg Roach 'tree' => $tree, 222e539f5c6SGreg Roach ]); 223e539f5c6SGreg Roach } 224e539f5c6SGreg Roach 225e539f5c6SGreg Roach /** 226e539f5c6SGreg Roach * Show a booklet view of ancestors 227e539f5c6SGreg Roach * 228e539f5c6SGreg Roach * @TODO replace ob_start() with views. 229e539f5c6SGreg Roach * 230e539f5c6SGreg Roach * @param Collection $ancestors 231e539f5c6SGreg Roach * @param bool $show_cousins 232e539f5c6SGreg Roach * 2336ccdf4f0SGreg Roach * @return ResponseInterface 234e539f5c6SGreg Roach */ 2356ccdf4f0SGreg Roach protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): ResponseInterface 236e539f5c6SGreg Roach { 237e539f5c6SGreg Roach ob_start(); 238e539f5c6SGreg Roach 239*f4ba05e3SGreg Roach echo view('chart-box', ['individual' => $ancestors[1]]); 240e539f5c6SGreg Roach foreach ($ancestors as $sosa => $individual) { 24139ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 242e539f5c6SGreg Roach FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins); 243e539f5c6SGreg Roach } 244e539f5c6SGreg Roach } 245e539f5c6SGreg Roach 246e539f5c6SGreg Roach $html = ob_get_clean(); 247e539f5c6SGreg Roach 2486ccdf4f0SGreg Roach return response($html); 249e539f5c6SGreg Roach } 250e539f5c6SGreg Roach 251e539f5c6SGreg Roach /** 252e539f5c6SGreg Roach * This chart can display its output in a number of styles 253e539f5c6SGreg Roach * 254e539f5c6SGreg Roach * @return array 255e539f5c6SGreg Roach */ 256e539f5c6SGreg Roach protected function chartStyles(): array 257e539f5c6SGreg Roach { 258e539f5c6SGreg Roach return [ 259e539f5c6SGreg Roach self::CHART_STYLE_LIST => I18N::translate('List'), 260e539f5c6SGreg Roach self::CHART_STYLE_BOOKLET => I18N::translate('Booklet'), 261e539f5c6SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 262e539f5c6SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 263e539f5c6SGreg Roach ]; 264e539f5c6SGreg Roach } 265168ff6f3Sric2016} 266