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