1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Module; 20 21use Fisharebest\Webtrees\Auth; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Individual; 24use Fisharebest\Webtrees\Menu; 25use Fisharebest\Webtrees\Services\ChartService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Support\Collection; 28use Psr\Http\Message\ResponseInterface; 29use Psr\Http\Message\ServerRequestInterface; 30 31use function view; 32 33/** 34 * Class AncestorsChartModule 35 */ 36class AncestorsChartModule extends AbstractModule implements ModuleChartInterface 37{ 38 use ModuleChartTrait; 39 40 // Chart styles 41 protected const CHART_STYLE_TREE = 'tree'; 42 protected const CHART_STYLE_INDIVIDUALS = 'individuals'; 43 protected const CHART_STYLE_FAMILIES = 'families'; 44 45 // Defaults 46 protected const DEFAULT_STYLE = self::CHART_STYLE_TREE; 47 protected const DEFAULT_GENERATIONS = '4'; 48 49 // Limits 50 protected const MINIMUM_GENERATIONS = 2; 51 protected const MAXIMUM_GENERATIONS = 10; 52 53 /** @var ChartService */ 54 private $chart_service; 55 56 /** 57 * CompactTreeChartModule constructor. 58 * 59 * @param ChartService $chart_service 60 */ 61 public function __construct(ChartService $chart_service) 62 { 63 $this->chart_service = $chart_service; 64 } 65 66 /** 67 * How should this module be identified in the control panel, etc.? 68 * 69 * @return string 70 */ 71 public function title(): string 72 { 73 /* I18N: Name of a module/chart */ 74 return I18N::translate('Ancestors'); 75 } 76 77 /** 78 * A sentence describing what this module does. 79 * 80 * @return string 81 */ 82 public function description(): string 83 { 84 /* I18N: Description of the “AncestorsChart” module */ 85 return I18N::translate('A chart of an individual’s ancestors.'); 86 } 87 88 /** 89 * CSS class for the URL. 90 * 91 * @return string 92 */ 93 public function chartMenuClass(): string 94 { 95 return 'menu-chart-ancestry'; 96 } 97 98 /** 99 * Return a menu item for this chart - for use in individual boxes. 100 * 101 * @param Individual $individual 102 * 103 * @return Menu|null 104 */ 105 public function chartBoxMenu(Individual $individual): ?Menu 106 { 107 return $this->chartMenu($individual); 108 } 109 110 /** 111 * The title for a specific instance of this chart. 112 * 113 * @param Individual $individual 114 * 115 * @return string 116 */ 117 public function chartTitle(Individual $individual): string 118 { 119 /* I18N: %s is an individual’s name */ 120 return I18N::translate('Ancestors of %s', $individual->fullName()); 121 } 122 123 /** 124 * A form to request the chart parameters. 125 * 126 * @param ServerRequestInterface $request 127 * 128 * @return ResponseInterface 129 */ 130 public function getChartAction(ServerRequestInterface $request): ResponseInterface 131 { 132 $tree = $request->getAttribute('tree'); 133 $user = $request->getAttribute('user'); 134 $ajax = $request->getQueryParams()['ajax'] ?? ''; 135 $xref = $request->getQueryParams()['xref'] ?? ''; 136 $individual = Individual::getInstance($xref, $tree); 137 138 Auth::checkIndividualAccess($individual); 139 Auth::checkComponentAccess($this, 'chart', $tree, $user); 140 141 $chart_style = $request->getQueryParams()['chart_style'] ?? self::DEFAULT_STYLE; 142 $generations = (int) ($request->getQueryParams()['generations'] ?? self::DEFAULT_GENERATIONS); 143 144 $generations = min($generations, self::MAXIMUM_GENERATIONS); 145 $generations = max($generations, self::MINIMUM_GENERATIONS); 146 147 if ($ajax === '1') { 148 $ancestors = $this->chart_service->sosaStradonitzAncestors($individual, $generations); 149 150 switch ($chart_style) { 151 default: 152 case self::CHART_STYLE_TREE: 153 return response(view('modules/ancestors-chart/tree', ['individual' => $individual, 'parents' => $individual->primaryChildFamily(), 'generations' => $generations, 'sosa' => 1])); 154 155 case self::CHART_STYLE_INDIVIDUALS: 156 return $this->ancestorsIndividuals($tree, $ancestors); 157 158 case self::CHART_STYLE_FAMILIES: 159 return $this->ancestorsFamilies($tree, $ancestors); 160 } 161 } 162 163 $ajax_url = $this->chartUrl($individual, [ 164 'generations' => $generations, 165 'chart_style' => $chart_style, 166 'ajax' => true, 167 ]); 168 169 return $this->viewResponse('modules/ancestors-chart/page', [ 170 'ajax_url' => $ajax_url, 171 'chart_style' => $chart_style, 172 'chart_styles' => $this->chartStyles(), 173 'default_generations' => self::DEFAULT_GENERATIONS, 174 'generations' => $generations, 175 'individual' => $individual, 176 'maximum_generations' => self::MAXIMUM_GENERATIONS, 177 'minimum_generations' => self::MINIMUM_GENERATIONS, 178 'module_name' => $this->name(), 179 'title' => $this->chartTitle($individual), 180 ]); 181 } 182 183 /** 184 * Show a tabular list of individual ancestors. 185 * 186 * @param Tree $tree 187 * @param Collection $ancestors 188 * 189 * @return ResponseInterface 190 */ 191 protected function ancestorsIndividuals(Tree $tree, Collection $ancestors): ResponseInterface 192 { 193 $this->layout = 'layouts/ajax'; 194 195 return $this->viewResponse('lists/individuals-table', [ 196 'individuals' => $ancestors, 197 'sosa' => true, 198 'tree' => $tree, 199 ]); 200 } 201 202 /** 203 * Show a tabular list of individual ancestors. 204 * 205 * @param Tree $tree 206 * @param Collection $ancestors 207 * 208 * @return ResponseInterface 209 */ 210 protected function ancestorsFamilies(Tree $tree, Collection $ancestors): ResponseInterface 211 { 212 $this->layout = 'layouts/ajax'; 213 214 $families = []; 215 foreach ($ancestors as $individual) { 216 foreach ($individual->childFamilies() as $family) { 217 $families[$family->xref()] = $family; 218 } 219 } 220 221 return $this->viewResponse('lists/families-table', [ 222 'families' => $families, 223 'tree' => $tree, 224 ]); 225 } 226 227 /** 228 * This chart can display its output in a number of styles 229 * 230 * @return array 231 */ 232 protected function chartStyles(): array 233 { 234 return [ 235 self::CHART_STYLE_TREE => I18N::translate('Tree'), 236 self::CHART_STYLE_INDIVIDUALS => I18N::translate('Individuals'), 237 self::CHART_STYLE_FAMILIES => I18N::translate('Families'), 238 ]; 239 } 240} 241