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; 31e539f5c6SGreg Roachuse Symfony\Component\HttpFoundation\Request; 32e539f5c6SGreg Roachuse Symfony\Component\HttpFoundation\Response; 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; 50e539f5c6SGreg Roach protected const DEFAULT_GENERATIONS = '3'; 51e539f5c6SGreg Roach protected const DEFAULT_MAXIMUM_GENERATIONS = '9'; 52e539f5c6SGreg Roach 53168ff6f3Sric2016 /** 54168ff6f3Sric2016 * How should this module be labelled on tabs, menus, etc.? 55168ff6f3Sric2016 * 56168ff6f3Sric2016 * @return string 57168ff6f3Sric2016 */ 5849a243cbSGreg Roach public function title(): string 59c1010edaSGreg Roach { 60bbb76c12SGreg Roach /* I18N: Name of a module/chart */ 61bbb76c12SGreg Roach return I18N::translate('Ancestors'); 62168ff6f3Sric2016 } 63168ff6f3Sric2016 64168ff6f3Sric2016 /** 65168ff6f3Sric2016 * A sentence describing what this module does. 66168ff6f3Sric2016 * 67168ff6f3Sric2016 * @return string 68168ff6f3Sric2016 */ 6949a243cbSGreg Roach public function description(): string 70c1010edaSGreg Roach { 71bbb76c12SGreg Roach /* I18N: Description of the “AncestorsChart” module */ 72bbb76c12SGreg Roach return I18N::translate('A chart of an individual’s ancestors.'); 73168ff6f3Sric2016 } 74168ff6f3Sric2016 75168ff6f3Sric2016 /** 76377a2979SGreg Roach * CSS class for the URL. 77377a2979SGreg Roach * 78377a2979SGreg Roach * @return string 79377a2979SGreg Roach */ 80377a2979SGreg Roach public function chartMenuClass(): string 81377a2979SGreg Roach { 82377a2979SGreg Roach return 'menu-chart-ancestry'; 83377a2979SGreg Roach } 84377a2979SGreg Roach 85377a2979SGreg Roach /** 864eb71cfaSGreg Roach * Return a menu item for this chart - for use in individual boxes. 874eb71cfaSGreg Roach * 8860bc3e3fSGreg Roach * @param Individual $individual 8960bc3e3fSGreg Roach * 904eb71cfaSGreg Roach * @return Menu|null 914eb71cfaSGreg Roach */ 92377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 93c1010edaSGreg Roach { 94e6562982SGreg Roach return $this->chartMenu($individual); 95e6562982SGreg Roach } 96e6562982SGreg Roach 97e6562982SGreg Roach /** 98e6562982SGreg Roach * The title for a specific instance of this chart. 99e6562982SGreg Roach * 100e6562982SGreg Roach * @param Individual $individual 101e6562982SGreg Roach * 102e6562982SGreg Roach * @return string 103e6562982SGreg Roach */ 104e6562982SGreg Roach public function chartTitle(Individual $individual): string 105e6562982SGreg Roach { 106e6562982SGreg Roach /* I18N: %s is an individual’s name */ 107e6562982SGreg Roach return I18N::translate('Ancestors of %s', $individual->getFullName()); 108e6562982SGreg Roach } 109e6562982SGreg Roach 110e6562982SGreg Roach /** 111e539f5c6SGreg Roach * A form to request the chart parameters. 112e539f5c6SGreg Roach * 113e539f5c6SGreg Roach * @param Request $request 114e539f5c6SGreg Roach * @param Tree $tree 115e5a6b4d4SGreg Roach * @param UserInterface $user 116e539f5c6SGreg Roach * @param ChartService $chart_service 117e539f5c6SGreg Roach * 118e539f5c6SGreg Roach * @return Response 119e539f5c6SGreg Roach */ 120e5a6b4d4SGreg Roach public function getChartAction(Request $request, Tree $tree, UserInterface $user, ChartService $chart_service): Response 121e539f5c6SGreg Roach { 1229b5537c3SGreg Roach $ajax = (bool) $request->get('ajax'); 123e539f5c6SGreg Roach $xref = $request->get('xref', ''); 124e539f5c6SGreg Roach $individual = Individual::getInstance($xref, $tree); 125e539f5c6SGreg Roach 126e539f5c6SGreg Roach Auth::checkIndividualAccess($individual); 1279867b2f0SGreg Roach Auth::checkComponentAccess($this, 'chart', $tree, $user); 128e539f5c6SGreg Roach 129e539f5c6SGreg Roach $minimum_generations = 2; 130e539f5c6SGreg Roach $maximum_generations = (int) $tree->getPreference('MAX_PEDIGREE_GENERATIONS', self::DEFAULT_MAXIMUM_GENERATIONS); 131e539f5c6SGreg Roach $default_generations = (int) $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS', self::DEFAULT_GENERATIONS); 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); 135e539f5c6SGreg Roach $generations = (int) $request->get('generations', $default_generations); 136e539f5c6SGreg Roach 137e539f5c6SGreg Roach $generations = min($generations, $maximum_generations); 138e539f5c6SGreg Roach $generations = max($generations, $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: 146e539f5c6SGreg Roach return $this->ancestorsList($individual, $generations); 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(), 170e539f5c6SGreg Roach 'default_generations' => $default_generations, 171e539f5c6SGreg Roach 'generations' => $generations, 172e539f5c6SGreg Roach 'individual' => $individual, 173e539f5c6SGreg Roach 'maximum_generations' => $maximum_generations, 174e539f5c6SGreg Roach 'minimum_generations' => $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 hierarchical list of ancestors 183e539f5c6SGreg Roach * 184e539f5c6SGreg Roach * @TODO replace ob_start() with views. 185e539f5c6SGreg Roach * 186e539f5c6SGreg Roach * @param Individual $individual 187e539f5c6SGreg Roach * @param int $generations 188e539f5c6SGreg Roach * 189e539f5c6SGreg Roach * @return Response 190e539f5c6SGreg Roach */ 191e539f5c6SGreg Roach protected function ancestorsList(Individual $individual, int $generations): Response 192e539f5c6SGreg Roach { 193e539f5c6SGreg Roach ob_start(); 194e539f5c6SGreg Roach 195e539f5c6SGreg Roach $this->printChildAscendancy($individual, 1, $generations - 1); 196e539f5c6SGreg Roach 197e539f5c6SGreg Roach $html = ob_get_clean(); 198e539f5c6SGreg Roach 199*242a7862SGreg Roach $html = '<ul class="wt-ancestors-chart-list list-unstyled">' . $html . '</ul>'; 200e539f5c6SGreg Roach 201e539f5c6SGreg Roach return new Response($html); 202e539f5c6SGreg Roach } 203e539f5c6SGreg Roach 204e539f5c6SGreg Roach /** 205e539f5c6SGreg Roach * print a child ascendancy 206e539f5c6SGreg Roach * 207e539f5c6SGreg Roach * @param Individual $individual 208e539f5c6SGreg Roach * @param int $sosa 209e539f5c6SGreg Roach * @param int $generations 210e539f5c6SGreg Roach * 211e539f5c6SGreg Roach * @return void 212e539f5c6SGreg Roach */ 213e539f5c6SGreg Roach protected function printChildAscendancy(Individual $individual, $sosa, $generations) 214e539f5c6SGreg Roach { 215*242a7862SGreg Roach echo '<li class="wt-chart-ancestors-list-item">'; 216e539f5c6SGreg Roach echo '<table><tbody><tr><td>'; 217e539f5c6SGreg Roach if ($sosa === 1) { 218e837ff07SGreg Roach echo '<img src="', e(asset('css/images/spacer.png')), '" height="3" width="15"></td><td>'; 219e539f5c6SGreg Roach } else { 220e837ff07SGreg Roach echo '<img src="', e(asset('css/images/spacer.png')), '" height="3" width="2">'; 221e837ff07SGreg Roach echo '<img src="', e(asset('css/images/hline.png')), '" height="3" width="13"></td><td>'; 222e539f5c6SGreg Roach } 223e539f5c6SGreg Roach echo FunctionsPrint::printPedigreePerson($individual); 224e539f5c6SGreg Roach echo '</td><td>'; 225e539f5c6SGreg Roach if ($sosa > 1) { 22639b853a7SGreg Roach echo '<a href="' . e($this->chartUrl($individual, ['generations' => $generations, 'chart_style' => self::CHART_STYLE_LIST])) . '" title="' . strip_tags($this->chartTitle($individual)) . '">' . view('icons/arrow-down') . '<span class="sr-only">' . $this->chartTitle($individual) . '</span></a>'; 227e539f5c6SGreg Roach } 228*242a7862SGreg Roach echo '</td><td class="details1"> <span class="wt-chart-box wt-chart-box-' . ($sosa === 1 ? strtolower($individual->getSex()) : ($sosa % 2 ? 'f' : 'm')) . '">', I18N::number($sosa), '</span> '; 229e539f5c6SGreg Roach echo '</td><td class="details1"> ', FunctionsCharts::getSosaName($sosa), '</td>'; 230e539f5c6SGreg Roach echo '</tr></tbody></table>'; 231e539f5c6SGreg Roach 232e539f5c6SGreg Roach // Parents 233e539f5c6SGreg Roach $family = $individual->getPrimaryChildFamily(); 234e539f5c6SGreg Roach if ($family && $generations > 0) { 235e539f5c6SGreg Roach // Marriage details 236e539f5c6SGreg Roach echo '<span class="details1">'; 237e837ff07SGreg Roach echo '<img src="', e(asset('css/images/spacer.png')), '" height="2" width="15"><a href="#" onclick="return expand_layer(\'sosa_', $sosa, '\');" class="top"><i id="sosa_', $sosa, '_img" class="icon-minus" title="', I18N::translate('View this family'), '"></i></a>'; 238*242a7862SGreg Roach echo ' <span class="wt-chart-box wt-chart-box-m">', I18N::number($sosa * 2), '</span> '; 239*242a7862SGreg Roach echo I18N::translate('and'); 240*242a7862SGreg Roach echo ' <span class="wt-chart-box wt-chart-box-f">', I18N::number($sosa * 2 + 1), '</span>'; 241e539f5c6SGreg Roach if ($family->canShow()) { 242e539f5c6SGreg Roach foreach ($family->facts(Gedcom::MARRIAGE_EVENTS) as $fact) { 243e539f5c6SGreg Roach echo ' <a href="', e($family->url()), '" class="details1">', $fact->summary(), '</a>'; 244e539f5c6SGreg Roach } 245e539f5c6SGreg Roach } 246e539f5c6SGreg Roach echo '</span>'; 247*242a7862SGreg Roach echo '<ul class="wt-chart-ancestors-list list-unstyled" id="sosa_', $sosa, '">'; 248e539f5c6SGreg Roach if ($family->getHusband()) { 249e539f5c6SGreg Roach $this->printChildAscendancy($family->getHusband(), $sosa * 2, $generations - 1); 250e539f5c6SGreg Roach } 251e539f5c6SGreg Roach if ($family->getWife()) { 252e539f5c6SGreg Roach $this->printChildAscendancy($family->getWife(), $sosa * 2 + 1, $generations - 1); 253e539f5c6SGreg Roach } 254e539f5c6SGreg Roach echo '</ul>'; 255e539f5c6SGreg Roach } 256e539f5c6SGreg Roach echo '</li>'; 257e539f5c6SGreg Roach } 258e539f5c6SGreg Roach 259e539f5c6SGreg Roach /** 260e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 261e539f5c6SGreg Roach * 262e539f5c6SGreg Roach * @param Tree $tree 263e539f5c6SGreg Roach * @param Collection $ancestors 264e539f5c6SGreg Roach * 265e539f5c6SGreg Roach * @return Response 266e539f5c6SGreg Roach */ 267e539f5c6SGreg Roach protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): Response 268e539f5c6SGreg Roach { 269e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 270e539f5c6SGreg Roach 271e539f5c6SGreg Roach return $this->viewResponse('lists/individuals-table', [ 272e539f5c6SGreg Roach 'individuals' => $ancestors, 273e539f5c6SGreg Roach 'sosa' => true, 274e539f5c6SGreg Roach 'tree' => $tree, 275e539f5c6SGreg Roach ]); 276e539f5c6SGreg Roach } 277e539f5c6SGreg Roach 278e539f5c6SGreg Roach /** 279e539f5c6SGreg Roach * Show a tabular list of individual ancestors. 280e539f5c6SGreg Roach * 281e539f5c6SGreg Roach * @param Tree $tree 282e539f5c6SGreg Roach * @param Collection $ancestors 283e539f5c6SGreg Roach * 284e539f5c6SGreg Roach * @return Response 285e539f5c6SGreg Roach */ 286e539f5c6SGreg Roach protected function ancestorsFamilies(Tree $tree, Collection $ancestors): Response 287e539f5c6SGreg Roach { 288e539f5c6SGreg Roach $this->layout = 'layouts/ajax'; 289e539f5c6SGreg Roach 290e539f5c6SGreg Roach $families = []; 291e539f5c6SGreg Roach foreach ($ancestors as $individual) { 292e539f5c6SGreg Roach foreach ($individual->getChildFamilies() as $family) { 293e539f5c6SGreg Roach $families[$family->xref()] = $family; 294e539f5c6SGreg Roach } 295e539f5c6SGreg Roach } 296e539f5c6SGreg Roach 297e539f5c6SGreg Roach return $this->viewResponse('lists/families-table', [ 298e539f5c6SGreg Roach 'families' => $families, 299e539f5c6SGreg Roach 'tree' => $tree, 300e539f5c6SGreg Roach ]); 301e539f5c6SGreg Roach } 302e539f5c6SGreg Roach 303e539f5c6SGreg Roach /** 304e539f5c6SGreg Roach * Show a booklet view of ancestors 305e539f5c6SGreg Roach * 306e539f5c6SGreg Roach * @TODO replace ob_start() with views. 307e539f5c6SGreg Roach * 308e539f5c6SGreg Roach * @param Collection $ancestors 309e539f5c6SGreg Roach * @param bool $show_cousins 310e539f5c6SGreg Roach * 311e539f5c6SGreg Roach * @return Response 312e539f5c6SGreg Roach */ 313e539f5c6SGreg Roach protected function ancestorsBooklet(Collection $ancestors, bool $show_cousins): Response 314e539f5c6SGreg Roach { 315e539f5c6SGreg Roach ob_start(); 316e539f5c6SGreg Roach 317e539f5c6SGreg Roach echo FunctionsPrint::printPedigreePerson($ancestors[1]); 318e539f5c6SGreg Roach foreach ($ancestors as $sosa => $individual) { 319e539f5c6SGreg Roach foreach ($individual->getChildFamilies() as $family) { 320e539f5c6SGreg Roach FunctionsCharts::printSosaFamily($family, $individual->xref(), $sosa, '', '', '', $show_cousins); 321e539f5c6SGreg Roach } 322e539f5c6SGreg Roach } 323e539f5c6SGreg Roach 324e539f5c6SGreg Roach $html = ob_get_clean(); 325e539f5c6SGreg Roach 326e539f5c6SGreg Roach return new Response($html); 327e539f5c6SGreg Roach } 328e539f5c6SGreg Roach 329e539f5c6SGreg Roach /** 330e539f5c6SGreg Roach * This chart can display its output in a number of styles 331e539f5c6SGreg Roach * 332e539f5c6SGreg Roach * @return array 333e539f5c6SGreg Roach */ 334e539f5c6SGreg Roach protected function chartStyles(): array 335e539f5c6SGreg Roach { 336e539f5c6SGreg Roach return [ 337e539f5c6SGreg Roach self::CHART_STYLE_LIST => I18N::translate('List'), 338e539f5c6SGreg Roach self::CHART_STYLE_BOOKLET => I18N::translate('Booklet'), 339e539f5c6SGreg Roach self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 340e539f5c6SGreg Roach self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 341e539f5c6SGreg Roach ]; 342e539f5c6SGreg Roach } 343168ff6f3Sric2016} 344