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