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