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 public function description(): string 113 { 114 /* I18N: Description of the “Pedigree map” module */ 115 return I18N::translate('Show the birthplace of ancestors on a map.'); 116 } 117 118 /** 119 * CSS class for the URL. 120 * 121 * @return string 122 */ 123 public function chartMenuClass(): string 124 { 125 return 'menu-chart-pedigreemap'; 126 } 127 128 /** 129 * Return a menu item for this chart - for use in individual boxes. 130 * 131 * @param Individual $individual 132 * 133 * @return Menu|null 134 */ 135 public function chartBoxMenu(Individual $individual): Menu|null 136 { 137 return $this->chartMenu($individual); 138 } 139 140 /** 141 * The title for a specific instance of this chart. 142 * 143 * @param Individual $individual 144 * 145 * @return string 146 */ 147 public function chartTitle(Individual $individual): string 148 { 149 /* I18N: %s is an individual’s name */ 150 return I18N::translate('Pedigree map of %s', $individual->fullName()); 151 } 152 153 /** 154 * The URL for a page showing chart options. 155 * 156 * @param Individual $individual 157 * @param array<bool|int|string|array<string>|null> $parameters 158 * 159 * @return string 160 */ 161 public function chartUrl(Individual $individual, array $parameters = []): string 162 { 163 return route(static::class, [ 164 'tree' => $individual->tree()->name(), 165 'xref' => $individual->xref(), 166 ] + $parameters + self::DEFAULT_PARAMETERS); 167 } 168 169 /** 170 * @param ServerRequestInterface $request 171 * 172 * @return ResponseInterface 173 */ 174 public function handle(ServerRequestInterface $request): ResponseInterface 175 { 176 $tree = Validator::attributes($request)->tree(); 177 $user = Validator::attributes($request)->user(); 178 $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 179 $xref = Validator::attributes($request)->isXref()->string('xref'); 180 181 // Convert POST requests into GET requests for pretty URLs. 182 if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 183 return redirect(route(static::class, [ 184 'tree' => $tree->name(), 185 'xref' => Validator::parsedBody($request)->isXref()->string('xref'), 186 'generations' => Validator::parsedBody($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'), 187 ])); 188 } 189 190 Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 191 192 $individual = Registry::individualFactory()->make($xref, $tree); 193 $individual = Auth::checkIndividualAccess($individual, false, true); 194 195 $map = view('modules/pedigree-map/chart', [ 196 'data' => $this->getMapData($request), 197 'leaflet_config' => $this->leaflet_js_service->config(), 198 ]); 199 200 return $this->viewResponse('modules/pedigree-map/page', [ 201 'module' => $this->name(), 202 /* I18N: %s is an individual’s name */ 203 'title' => I18N::translate('Pedigree map of %s', $individual->fullName()), 204 'tree' => $tree, 205 'individual' => $individual, 206 'generations' => $generations, 207 'maxgenerations' => self::MAXIMUM_GENERATIONS, 208 'map' => $map, 209 ]); 210 } 211 212 /** 213 * @param ServerRequestInterface $request 214 * 215 * @return array<mixed> $geojson 216 */ 217 protected function getMapData(ServerRequestInterface $request): array 218 { 219 $facts = $this->getPedigreeMapFacts($request, $this->chart_service); 220 221 $geojson = [ 222 'type' => 'FeatureCollection', 223 'features' => [], 224 ]; 225 226 $sosa_points = []; 227 228 foreach ($facts as $sosa => $fact) { 229 $location = new PlaceLocation($fact->place()->gedcomName()); 230 231 // Use the co-ordinates from the fact (if they exist). 232 $latitude = $fact->latitude(); 233 $longitude = $fact->longitude(); 234 235 // Use the co-ordinates from the location otherwise. 236 if ($latitude === null || $longitude === null) { 237 $latitude = $location->latitude(); 238 $longitude = $location->longitude(); 239 } 240 241 if ($latitude !== null && $longitude !== null) { 242 $polyline = null; 243 $sosa_points[$sosa] = [$latitude, $longitude]; 244 $sosa_child = intdiv($sosa, 2); 245 $generation = (int) log($sosa, 2); 246 $color = 'var(--wt-pedigree-map-gen-' . $generation % self::COUNT_CSS_COLORS . ')'; 247 $class = 'wt-pedigree-map-gen-' . $generation % self::COUNT_CSS_COLORS; 248 249 if (array_key_exists($sosa_child, $sosa_points)) { 250 // Would like to use a GeometryCollection to hold LineStrings 251 // rather than generate polylines but the MarkerCluster library 252 // doesn't seem to like them 253 $polyline = [ 254 'points' => [ 255 $sosa_points[$sosa_child], 256 [$latitude, $longitude], 257 ], 258 'options' => [ 259 'color' => $color, 260 ], 261 ]; 262 } 263 $geojson['features'][] = [ 264 'type' => 'Feature', 265 'id' => $sosa, 266 'geometry' => [ 267 'type' => 'Point', 268 'coordinates' => [$longitude, $latitude], 269 ], 270 'properties' => [ 271 'polyline' => $polyline, 272 'iconcolor' => $color, 273 'tooltip' => null, 274 'summary' => view('modules/pedigree-map/events', [ 275 'class' => $class, 276 'fact' => $fact, 277 'relationship' => $this->getSosaName($sosa), 278 'sosa' => $sosa, 279 ]), 280 ], 281 ]; 282 } 283 } 284 285 return $geojson; 286 } 287 288 /** 289 * @param ServerRequestInterface $request 290 * @param ChartService $chart_service 291 * 292 * @return array<Fact> 293 */ 294 protected function getPedigreeMapFacts(ServerRequestInterface $request, ChartService $chart_service): array 295 { 296 $tree = Validator::attributes($request)->tree(); 297 $generations = Validator::attributes($request)->isBetween(self::MINIMUM_GENERATIONS, self::MAXIMUM_GENERATIONS)->integer('generations'); 298 $xref = Validator::attributes($request)->isXref()->string('xref'); 299 $individual = Registry::individualFactory()->make($xref, $tree); 300 $individual = Auth::checkIndividualAccess($individual, false, true); 301 $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); 302 $facts = []; 303 304 foreach ($ancestors as $sosa => $person) { 305 if ($person->canShow()) { 306 $birth = $person->facts(Gedcom::BIRTH_EVENTS, true) 307 ->first(static fn (Fact $fact): bool => $fact->place()->gedcomName() !== ''); 308 309 if ($birth instanceof Fact) { 310 $facts[$sosa] = $birth; 311 } 312 } 313 } 314 315 return $facts; 316 } 317 318 /** 319 * builds and returns sosa relationship name in the active language 320 * 321 * @param int $sosa Sosa number 322 * 323 * @return string 324 */ 325 protected function getSosaName(int $sosa): string 326 { 327 $path = ''; 328 329 while ($sosa > 1) { 330 if ($sosa % 2 === 1) { 331 $path = 'mot' . $path; 332 } else { 333 $path = 'fat' . $path; 334 } 335 $sosa = intdiv($sosa, 2); 336 } 337 338 return ucfirst($this->relationship_service->legacyNameAlgorithm($path)); 339 } 340} 341