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 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Aura\Router\RouterContainer; 23use Fig\Http\Message\RequestMethodInterface; 24use Fig\Http\Message\StatusCodeInterface; 25use Fisharebest\Webtrees\Auth; 26use Fisharebest\Webtrees\Fact; 27use Fisharebest\Webtrees\Family; 28use Fisharebest\Webtrees\Functions\Functions; 29use Fisharebest\Webtrees\GedcomTag; 30use Fisharebest\Webtrees\I18N; 31use Fisharebest\Webtrees\Individual; 32use Fisharebest\Webtrees\Location; 33use Fisharebest\Webtrees\Menu; 34use Fisharebest\Webtrees\Services\ChartService; 35use Psr\Http\Message\ResponseInterface; 36use Psr\Http\Message\ServerRequestInterface; 37use Psr\Http\Server\RequestHandlerInterface; 38 39use function intdiv; 40use function redirect; 41use function route; 42use function view; 43 44/** 45 * Class PedigreeMapModule 46 */ 47class PedigreeMapModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 48{ 49 use ModuleChartTrait; 50 51 private const ROUTE_NAME = 'pedigree-map'; 52 private const ROUTE_URL = '/tree/{tree}/pedigree-map-{generations}/{xref}'; 53 54 // Defaults 55 public const DEFAULT_GENERATIONS = '4'; 56 public const DEFAULT_PARAMETERS = [ 57 'generations' => self::DEFAULT_GENERATIONS, 58 ]; 59 60 // Limits 61 public const MAXIMUM_GENERATIONS = 10; 62 63 private const LINE_COLORS = [ 64 '#FF0000', 65 // Red 66 '#00FF00', 67 // Green 68 '#0000FF', 69 // Blue 70 '#FFB300', 71 // Gold 72 '#00FFFF', 73 // Cyan 74 '#FF00FF', 75 // Purple 76 '#7777FF', 77 // Light blue 78 '#80FF80' 79 // Light green 80 ]; 81 82 /** @var ChartService */ 83 private $chart_service; 84 85 /** 86 * PedigreeMapModule constructor. 87 * 88 * @param ChartService $chart_service 89 */ 90 public function __construct(ChartService $chart_service) 91 { 92 $this->chart_service = $chart_service; 93 } 94 95 /** 96 * Initialization. 97 * 98 * @param RouterContainer $router_container 99 */ 100 public function boot(RouterContainer $router_container): void 101 { 102 $router_container->getMap() 103 ->get(self::ROUTE_NAME, self::ROUTE_URL, self::class) 104 ->allows(RequestMethodInterface::METHOD_POST) 105 ->tokens([ 106 'generations' => '\d+', 107 ]); 108 } 109 110 /** 111 * How should this module be identified in the control panel, etc.? 112 * 113 * @return string 114 */ 115 public function title(): string 116 { 117 /* I18N: Name of a module */ 118 return I18N::translate('Pedigree map'); 119 } 120 121 /** 122 * A sentence describing what this module does. 123 * 124 * @return string 125 */ 126 public function description(): string 127 { 128 /* I18N: Description of the “Pedigree map” module */ 129 return I18N::translate('Show the birthplace of ancestors on a map.'); 130 } 131 132 /** 133 * CSS class for the URL. 134 * 135 * @return string 136 */ 137 public function chartMenuClass(): string 138 { 139 return 'menu-chart-pedigreemap'; 140 } 141 142 /** 143 * Return a menu item for this chart - for use in individual boxes. 144 * 145 * @param Individual $individual 146 * 147 * @return Menu|null 148 */ 149 public function chartBoxMenu(Individual $individual): ?Menu 150 { 151 return $this->chartMenu($individual); 152 } 153 154 /** 155 * The title for a specific instance of this chart. 156 * 157 * @param Individual $individual 158 * 159 * @return string 160 */ 161 public function chartTitle(Individual $individual): string 162 { 163 /* I18N: %s is an individual’s name */ 164 return I18N::translate('Pedigree map of %s', $individual->fullName()); 165 } 166 167 /** 168 * The URL for a page showing chart options. 169 * 170 * @param Individual $individual 171 * @param mixed[] $parameters 172 * 173 * @return string 174 */ 175 public function chartUrl(Individual $individual, array $parameters = []): string 176 { 177 return route(self::ROUTE_NAME, [ 178 'tree' => $individual->tree()->name(), 179 'xref' => $individual->xref(), 180 ] + $parameters + self::DEFAULT_PARAMETERS); 181 } 182 183 /** 184 * @param ServerRequestInterface $request 185 * 186 * @return ResponseInterface 187 */ 188 public function getMapDataAction(ServerRequestInterface $request): ResponseInterface 189 { 190 $ajax = $request->getQueryParams()['ajax'] ?? ''; 191 $generations = (int) $request->getAttribute('generations'); 192 $tree = $request->getAttribute('tree'); 193 $user = $request->getAttribute('user'); 194 $xref = $request->getQueryParams()['xref']; 195 $individual = Individual::getInstance($xref, $tree); 196 $color_count = count(self::LINE_COLORS); 197 198 $facts = $this->getPedigreeMapFacts($request, $this->chart_service); 199 200 $geojson = [ 201 'type' => 'FeatureCollection', 202 'features' => [], 203 ]; 204 205 $sosa_points = []; 206 207 foreach ($facts as $id => $fact) { 208 $location = new Location($fact->place()->gedcomName()); 209 210 // Use the co-ordinates from the fact (if they exist). 211 $latitude = $fact->latitude(); 212 $longitude = $fact->longitude(); 213 214 // Use the co-ordinates from the location otherwise. 215 if ($latitude === 0.0 && $longitude === 0.0) { 216 $latitude = $location->latitude(); 217 $longitude = $location->longitude(); 218 } 219 220 $icon = ['color' => 'Gold', 'name' => 'bullseye ']; 221 if ($latitude !== 0.0 || $longitude !== 0.0) { 222 $polyline = null; 223 $color = self::LINE_COLORS[log($id, 2) % $color_count]; 224 $icon['color'] = $color; //make icon color the same as the line 225 $sosa_points[$id] = [$latitude, $longitude]; 226 $sosa_parent = intdiv($id, 2); 227 if (array_key_exists($sosa_parent, $sosa_points)) { 228 // Would like to use a GeometryCollection to hold LineStrings 229 // rather than generate polylines but the MarkerCluster library 230 // doesn't seem to like them 231 $polyline = [ 232 'points' => [ 233 $sosa_points[$sosa_parent], 234 [$latitude, $longitude], 235 ], 236 'options' => [ 237 'color' => $color, 238 ], 239 ]; 240 } 241 $geojson['features'][] = [ 242 'type' => 'Feature', 243 'id' => $id, 244 'valid' => true, 245 'geometry' => [ 246 'type' => 'Point', 247 'coordinates' => [$longitude, $latitude], 248 ], 249 'properties' => [ 250 'polyline' => $polyline, 251 'icon' => $icon, 252 'tooltip' => strip_tags($fact->place()->fullName()), 253 'summary' => view('modules/pedigree-map/events', $this->summaryData($individual, $fact, $id)), 254 'zoom' => $location->zoom() ?: 2, 255 ], 256 ]; 257 } 258 } 259 260 $code = empty($facts) ? StatusCodeInterface::STATUS_NO_CONTENT : StatusCodeInterface::STATUS_OK; 261 262 return response($geojson, $code); 263 } 264 265 /** 266 * @param ServerRequestInterface $request 267 * 268 * @return ResponseInterface 269 */ 270 public function handle(ServerRequestInterface $request): ResponseInterface 271 { 272 $generations = (int) $request->getAttribute('generations'); 273 $tree = $request->getAttribute('tree'); 274 $user = $request->getAttribute('user'); 275 $xref = $request->getAttribute('xref'); 276 $individual = Individual::getInstance($xref, $tree); 277 278 // Convert POST requests into GET requests for pretty URLs. 279 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 280 return redirect(route(self::ROUTE_NAME, [ 281 'tree' => $tree->name(), 282 'xref' => $request->getParsedBody()['xref'], 283 'generations' => $request->getParsedBody()['generations'], 284 ])); 285 } 286 287 Auth::checkIndividualAccess($individual); 288 Auth::checkComponentAccess($this, 'chart', $tree, $user); 289 290 $map = view('modules/pedigree-map/chart', [ 291 'module' => $this->name(), 292 'individual' => $individual, 293 'type' => 'pedigree', 294 'generations' => $generations, 295 ]); 296 297 return $this->viewResponse('modules/pedigree-map/page', [ 298 'module' => $this->name(), 299 /* I18N: %s is an individual’s name */ 300 'title' => I18N::translate('Pedigree map of %s', $individual->fullName()), 301 'tree' => $tree, 302 'individual' => $individual, 303 'generations' => $generations, 304 'maxgenerations' => self::MAXIMUM_GENERATIONS, 305 'map' => $map, 306 ]); 307 } 308 309 /** 310 * @param ServerRequestInterface $request 311 * @param ChartService $chart_service 312 * 313 * @return array 314 */ 315 private function getPedigreeMapFacts(ServerRequestInterface $request, ChartService $chart_service): array 316 { 317 $generations = (int) $request->getAttribute('generations'); 318 $tree = $request->getAttribute('tree'); 319 $xref = $request->getQueryParams()['xref']; 320 $individual = Individual::getInstance($xref, $tree); 321 $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); 322 $facts = []; 323 foreach ($ancestors as $sosa => $person) { 324 if ($person->canShow()) { 325 $birth = $person->facts(['BIRT'])->first(); 326 if ($birth instanceof Fact && $birth->place()->gedcomName() !== '') { 327 $facts[$sosa] = $birth; 328 } 329 } 330 } 331 332 return $facts; 333 } 334 335 /** 336 * @param Individual $individual 337 * @param Fact $fact 338 * @param int $sosa 339 * 340 * @return array 341 */ 342 private function summaryData(Individual $individual, Fact $fact, int $sosa): array 343 { 344 $record = $fact->record(); 345 $name = ''; 346 $url = ''; 347 $tag = $fact->label(); 348 $addbirthtag = false; 349 350 if ($record instanceof Family) { 351 // Marriage 352 $spouse = $record->spouse($individual); 353 if ($spouse) { 354 $url = $spouse->url(); 355 $name = $spouse->fullName(); 356 } 357 } elseif ($record !== $individual) { 358 // Birth of a child 359 $url = $record->url(); 360 $name = $record->fullName(); 361 $tag = GedcomTag::getLabel('_BIRT_CHIL', $record); 362 } 363 364 if ($sosa > 1) { 365 $addbirthtag = true; 366 $tag = ucfirst($this->getSosaName($sosa)); 367 } 368 369 return [ 370 'tag' => $tag, 371 'url' => $url, 372 'name' => $name, 373 'value' => $fact->value(), 374 'date' => $fact->date()->display(true), 375 'place' => $fact->place(), 376 'addtag' => $addbirthtag, 377 ]; 378 } 379 380 /** 381 * builds and returns sosa relationship name in the active language 382 * 383 * @param int $sosa Sosa number 384 * 385 * @return string 386 */ 387 private function getSosaName(int $sosa): string 388 { 389 $path = ''; 390 391 while ($sosa > 1) { 392 if ($sosa % 2 === 1) { 393 $path = 'mot' . $path; 394 } else { 395 $path = 'fat' . $path; 396 } 397 $sosa = intdiv($sosa, 2); 398 } 399 400 return Functions::getRelationshipNameFromPath($path); 401 } 402} 403