11f374598SGreg Roach<?php 23976b470SGreg Roach 31f374598SGreg Roach/** 41f374598SGreg Roach * webtrees: online genealogy 590949315SGreg Roach * Copyright (C) 2021 webtrees development team 61f374598SGreg Roach * This program is free software: you can redistribute it and/or modify 71f374598SGreg Roach * it under the terms of the GNU General Public License as published by 81f374598SGreg Roach * the Free Software Foundation, either version 3 of the License, or 91f374598SGreg Roach * (at your option) any later version. 101f374598SGreg Roach * This program is distributed in the hope that it will be useful, 111f374598SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 121f374598SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131f374598SGreg Roach * GNU General Public License for more details. 141f374598SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 161f374598SGreg Roach */ 17fcfa147eSGreg Roach 181f374598SGreg Roachdeclare(strict_types=1); 191f374598SGreg Roach 201f374598SGreg Roachnamespace Fisharebest\Webtrees\Module; 211f374598SGreg Roach 2271378461SGreg Roachuse Aura\Router\RouterContainer; 2371378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 2471378461SGreg Roachuse Fisharebest\Webtrees\Auth; 251f374598SGreg Roachuse Fisharebest\Webtrees\Fact; 26752e4449SGreg Roachuse Fisharebest\Webtrees\Gedcom; 271f374598SGreg Roachuse Fisharebest\Webtrees\I18N; 281f374598SGreg Roachuse Fisharebest\Webtrees\Individual; 291f374598SGreg Roachuse Fisharebest\Webtrees\Menu; 30c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\PlaceLocation; 31c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Registry; 32aca28033SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 33c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService; 346fcafd02SGreg Roachuse Fisharebest\Webtrees\Services\RelationshipService; 354ea62551SGreg Roachuse Fisharebest\Webtrees\Tree; 366ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 376ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 3871378461SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 39752e4449SGreg Roach 409e18e23bSGreg Roachuse function app; 41c9a496a6SGreg Roachuse function array_key_exists; 429e18e23bSGreg Roachuse function assert; 43c9a496a6SGreg Roachuse function count; 44abaef046SGreg Roachuse function intdiv; 45ddeb3354SGreg Roachuse function is_string; 4611b6f9c6SGreg Roachuse function redirect; 4711b6f9c6SGreg Roachuse function route; 48c9a496a6SGreg Roachuse function ucfirst; 4971378461SGreg Roachuse function view; 501f374598SGreg Roach 511f374598SGreg Roach/** 521f374598SGreg Roach * Class PedigreeMapModule 531f374598SGreg Roach */ 5471378461SGreg Roachclass PedigreeMapModule extends AbstractModule implements ModuleChartInterface, RequestHandlerInterface 551f374598SGreg Roach{ 5649a243cbSGreg Roach use ModuleChartTrait; 5749a243cbSGreg Roach 5872f04adfSGreg Roach protected const ROUTE_URL = '/tree/{tree}/pedigree-map-{generations}/{xref}'; 5971378461SGreg Roach 60e759aebbSGreg Roach // Defaults 61e759aebbSGreg Roach public const DEFAULT_GENERATIONS = '4'; 6271378461SGreg Roach public const DEFAULT_PARAMETERS = [ 6371378461SGreg Roach 'generations' => self::DEFAULT_GENERATIONS, 6471378461SGreg Roach ]; 65e759aebbSGreg Roach 66e759aebbSGreg Roach // Limits 67e759aebbSGreg Roach public const MAXIMUM_GENERATIONS = 10; 68e759aebbSGreg Roach 69*0ef51d00SGreg Roach // CSS colors defined for each generation 70*0ef51d00SGreg Roach private const GENERATION_COLORS = 12; 71c9a496a6SGreg Roach 72c9c6f2ecSGreg Roach private ChartService $chart_service; 73c9c6f2ecSGreg Roach 74c9c6f2ecSGreg Roach private LeafletJsService $leaflet_js_service; 7557ab2231SGreg Roach 7657ab2231SGreg Roach /** 7757ab2231SGreg Roach * PedigreeMapModule constructor. 7857ab2231SGreg Roach * 7957ab2231SGreg Roach * @param ChartService $chart_service 80c9c6f2ecSGreg Roach * @param LeafletJsService $leaflet_js_service 8157ab2231SGreg Roach */ 82c9c6f2ecSGreg Roach public function __construct(ChartService $chart_service, LeafletJsService $leaflet_js_service) 833976b470SGreg Roach { 8457ab2231SGreg Roach $this->chart_service = $chart_service; 85c9c6f2ecSGreg Roach $this->leaflet_js_service = $leaflet_js_service; 8657ab2231SGreg Roach } 8757ab2231SGreg Roach 88961ec755SGreg Roach /** 8971378461SGreg Roach * Initialization. 9071378461SGreg Roach * 919e18e23bSGreg Roach * @return void 9271378461SGreg Roach */ 939e18e23bSGreg Roach public function boot(): void 9471378461SGreg Roach { 959e18e23bSGreg Roach $router_container = app(RouterContainer::class); 969e18e23bSGreg Roach assert($router_container instanceof RouterContainer); 979e18e23bSGreg Roach 9871378461SGreg Roach $router_container->getMap() 9972f04adfSGreg Roach ->get(static::class, static::ROUTE_URL, $this) 10071378461SGreg Roach ->allows(RequestMethodInterface::METHOD_POST) 10171378461SGreg Roach ->tokens([ 10271378461SGreg Roach 'generations' => '\d+', 10371378461SGreg Roach ]); 10471378461SGreg Roach } 10571378461SGreg Roach 10671378461SGreg Roach /** 1070cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 108961ec755SGreg Roach * 109961ec755SGreg Roach * @return string 110961ec755SGreg Roach */ 11149a243cbSGreg Roach public function title(): string 1121f374598SGreg Roach { 113bbb76c12SGreg Roach /* I18N: Name of a module */ 114bbb76c12SGreg Roach return I18N::translate('Pedigree map'); 1151f374598SGreg Roach } 1161f374598SGreg Roach 117961ec755SGreg Roach /** 118961ec755SGreg Roach * A sentence describing what this module does. 119961ec755SGreg Roach * 120961ec755SGreg Roach * @return string 121961ec755SGreg Roach */ 12249a243cbSGreg Roach public function description(): string 1231f374598SGreg Roach { 12471378461SGreg Roach /* I18N: Description of the “Pedigree map” module */ 125bbb76c12SGreg Roach return I18N::translate('Show the birthplace of ancestors on a map.'); 1261f374598SGreg Roach } 1271f374598SGreg Roach 1281f374598SGreg Roach /** 129377a2979SGreg Roach * CSS class for the URL. 130377a2979SGreg Roach * 131377a2979SGreg Roach * @return string 132377a2979SGreg Roach */ 133377a2979SGreg Roach public function chartMenuClass(): string 134377a2979SGreg Roach { 135377a2979SGreg Roach return 'menu-chart-pedigreemap'; 136377a2979SGreg Roach } 137377a2979SGreg Roach 138377a2979SGreg Roach /** 1391f374598SGreg Roach * Return a menu item for this chart - for use in individual boxes. 1401f374598SGreg Roach * 1411f374598SGreg Roach * @param Individual $individual 1421f374598SGreg Roach * 14349a243cbSGreg Roach * @return Menu|null 1441f374598SGreg Roach */ 145377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 1461f374598SGreg Roach { 147e6562982SGreg Roach return $this->chartMenu($individual); 148e6562982SGreg Roach } 149e6562982SGreg Roach 150e6562982SGreg Roach /** 151e6562982SGreg Roach * The title for a specific instance of this chart. 152e6562982SGreg Roach * 153e6562982SGreg Roach * @param Individual $individual 154e6562982SGreg Roach * 155e6562982SGreg Roach * @return string 156e6562982SGreg Roach */ 157e6562982SGreg Roach public function chartTitle(Individual $individual): string 158e6562982SGreg Roach { 159e6562982SGreg Roach /* I18N: %s is an individual’s name */ 16039ca88baSGreg Roach return I18N::translate('Pedigree map of %s', $individual->fullName()); 161e6562982SGreg Roach } 162e6562982SGreg Roach 163e6562982SGreg Roach /** 16471378461SGreg Roach * The URL for a page showing chart options. 165e6562982SGreg Roach * 166e6562982SGreg Roach * @param Individual $individual 16709482a55SGreg Roach * @param array<bool|int|string|array|null> $parameters 168e6562982SGreg Roach * 169e6562982SGreg Roach * @return string 170e6562982SGreg Roach */ 171e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 172e6562982SGreg Roach { 17372f04adfSGreg Roach return route(static::class, [ 17471378461SGreg Roach 'tree' => $individual->tree()->name(), 175e6562982SGreg Roach 'xref' => $individual->xref(), 17671378461SGreg Roach ] + $parameters + self::DEFAULT_PARAMETERS); 177e6562982SGreg Roach } 178e6562982SGreg Roach 179e6562982SGreg Roach /** 1806ccdf4f0SGreg Roach * @param ServerRequestInterface $request 1811f374598SGreg Roach * 1826ccdf4f0SGreg Roach * @return ResponseInterface 1831f374598SGreg Roach */ 18498579324SDavid Drury public function handle(ServerRequestInterface $request): ResponseInterface 18598579324SDavid Drury { 18698579324SDavid Drury $tree = $request->getAttribute('tree'); 18798579324SDavid Drury assert($tree instanceof Tree); 18898579324SDavid Drury 18998579324SDavid Drury $xref = $request->getAttribute('xref'); 19098579324SDavid Drury assert(is_string($xref)); 19198579324SDavid Drury 1926b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 193cf9da572SGreg Roach $individual = Auth::checkIndividualAccess($individual, false, true); 19498579324SDavid Drury 19598579324SDavid Drury $user = $request->getAttribute('user'); 19698579324SDavid Drury $generations = (int) $request->getAttribute('generations'); 197ef483801SGreg Roach Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user); 19898579324SDavid Drury 19998579324SDavid Drury // Convert POST requests into GET requests for pretty URLs. 20098579324SDavid Drury if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { 20198579324SDavid Drury $params = (array) $request->getParsedBody(); 20298579324SDavid Drury 20398579324SDavid Drury return redirect(route(static::class, [ 20498579324SDavid Drury 'tree' => $tree->name(), 20598579324SDavid Drury 'xref' => $params['xref'], 20698579324SDavid Drury 'generations' => $params['generations'], 20798579324SDavid Drury ])); 20898579324SDavid Drury } 20998579324SDavid Drury 21098579324SDavid Drury $map = view('modules/pedigree-map/chart', [ 21198579324SDavid Drury 'data' => $this->getMapData($request), 212c9c6f2ecSGreg Roach 'leaflet_config' => $this->leaflet_js_service->config(), 21398579324SDavid Drury ]); 21498579324SDavid Drury 21598579324SDavid Drury return $this->viewResponse('modules/pedigree-map/page', [ 21698579324SDavid Drury 'module' => $this->name(), 21798579324SDavid Drury /* I18N: %s is an individual’s name */ 21898579324SDavid Drury 'title' => I18N::translate('Pedigree map of %s', $individual->fullName()), 21998579324SDavid Drury 'tree' => $tree, 22098579324SDavid Drury 'individual' => $individual, 22198579324SDavid Drury 'generations' => $generations, 22298579324SDavid Drury 'maxgenerations' => self::MAXIMUM_GENERATIONS, 22398579324SDavid Drury 'map' => $map, 22498579324SDavid Drury ]); 22598579324SDavid Drury } 22698579324SDavid Drury 22798579324SDavid Drury /** 22898579324SDavid Drury * @param ServerRequestInterface $request 22998579324SDavid Drury * 23029eb5762SGreg Roach * @return array<mixed> $geojson 23198579324SDavid Drury */ 23298579324SDavid Drury private function getMapData(ServerRequestInterface $request): array 2331f374598SGreg Roach { 23457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2354ea62551SGreg Roach assert($tree instanceof Tree); 2364ea62551SGreg Roach 23757ab2231SGreg Roach $facts = $this->getPedigreeMapFacts($request, $this->chart_service); 2381f374598SGreg Roach 2391f374598SGreg Roach $geojson = [ 2401f374598SGreg Roach 'type' => 'FeatureCollection', 2411f374598SGreg Roach 'features' => [], 2421f374598SGreg Roach ]; 2437d988ec3SGreg Roach 2447d988ec3SGreg Roach $sosa_points = []; 2457d988ec3SGreg Roach 246620da733SGreg Roach foreach ($facts as $sosa => $fact) { 2475333da53SGreg Roach $location = new PlaceLocation($fact->place()->gedcomName()); 2488af6bbf8SGreg Roach 2498af6bbf8SGreg Roach // Use the co-ordinates from the fact (if they exist). 2508af6bbf8SGreg Roach $latitude = $fact->latitude(); 2518af6bbf8SGreg Roach $longitude = $fact->longitude(); 2528af6bbf8SGreg Roach 2538af6bbf8SGreg Roach // Use the co-ordinates from the location otherwise. 25490949315SGreg Roach if ($latitude === null || $longitude === null) { 2558af6bbf8SGreg Roach $latitude = $location->latitude(); 2568af6bbf8SGreg Roach $longitude = $location->longitude(); 2578af6bbf8SGreg Roach } 2588af6bbf8SGreg Roach 25990949315SGreg Roach if ($latitude !== null && $longitude !== null) { 2601f374598SGreg Roach $polyline = null; 261620da733SGreg Roach $sosa_points[$sosa] = [$latitude, $longitude]; 262620da733SGreg Roach $sosa_child = intdiv($sosa, 2); 263*0ef51d00SGreg Roach $color = 'var(--wt-pedigree-map-gen-' . $sosa_child % self::GENERATION_COLORS . ')'; 2640d123f04SDavid Drury 2650d123f04SDavid Drury if (array_key_exists($sosa_child, $sosa_points)) { 2661f374598SGreg Roach // Would like to use a GeometryCollection to hold LineStrings 2671f374598SGreg Roach // rather than generate polylines but the MarkerCluster library 2681f374598SGreg Roach // doesn't seem to like them 2691f374598SGreg Roach $polyline = [ 2701f374598SGreg Roach 'points' => [ 2710d123f04SDavid Drury $sosa_points[$sosa_child], 2728af6bbf8SGreg Roach [$latitude, $longitude], 2731f374598SGreg Roach ], 2741f374598SGreg Roach 'options' => [ 2751f374598SGreg Roach 'color' => $color, 2761f374598SGreg Roach ], 2771f374598SGreg Roach ]; 2781f374598SGreg Roach } 2791f374598SGreg Roach $geojson['features'][] = [ 2801f374598SGreg Roach 'type' => 'Feature', 281620da733SGreg Roach 'id' => $sosa, 2821f374598SGreg Roach 'geometry' => [ 2831f374598SGreg Roach 'type' => 'Point', 2848af6bbf8SGreg Roach 'coordinates' => [$longitude, $latitude], 2851f374598SGreg Roach ], 2861f374598SGreg Roach 'properties' => [ 2871f374598SGreg Roach 'polyline' => $polyline, 2883130efd4SGreg Roach 'iconcolor' => $color, 289497231cdSGreg Roach 'tooltip' => $fact->place()->gedcomName(), 290620da733SGreg Roach 'summary' => view('modules/pedigree-map/events', [ 291620da733SGreg Roach 'fact' => $fact, 292620da733SGreg Roach 'relationship' => ucfirst($this->getSosaName($sosa)), 293620da733SGreg Roach 'sosa' => $sosa, 294620da733SGreg Roach ]), 2951f374598SGreg Roach ], 2961f374598SGreg Roach ]; 2971f374598SGreg Roach } 2981f374598SGreg Roach } 2997d988ec3SGreg Roach 30098579324SDavid Drury return $geojson; 30171378461SGreg Roach } 30271378461SGreg Roach 30371378461SGreg Roach /** 30471378461SGreg Roach * @param ServerRequestInterface $request 3056ccdf4f0SGreg Roach * @param ChartService $chart_service 3066ccdf4f0SGreg Roach * 307fc26b4f6SGreg Roach * @return array<Fact> 3086ccdf4f0SGreg Roach */ 30957ab2231SGreg Roach private function getPedigreeMapFacts(ServerRequestInterface $request, ChartService $chart_service): array 3106ccdf4f0SGreg Roach { 31157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3124ea62551SGreg Roach assert($tree instanceof Tree); 3134ea62551SGreg Roach 31498579324SDavid Drury $generations = (int) $request->getAttribute('generations'); 31598579324SDavid Drury $xref = $request->getAttribute('xref'); 3166b9cb339SGreg Roach $individual = Registry::individualFactory()->make($xref, $tree); 3176ccdf4f0SGreg Roach $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); 3186ccdf4f0SGreg Roach $facts = []; 3196ccdf4f0SGreg Roach foreach ($ancestors as $sosa => $person) { 3206ccdf4f0SGreg Roach if ($person->canShow()) { 321752e4449SGreg Roach $birth = $person->facts(Gedcom::BIRTH_EVENTS, true) 32219d319e7SGreg Roach ->first(static function (Fact $fact): bool { 323752e4449SGreg Roach return $fact->place()->gedcomName() !== ''; 32419d319e7SGreg Roach }); 325752e4449SGreg Roach 326752e4449SGreg Roach if ($birth instanceof Fact) { 3276ccdf4f0SGreg Roach $facts[$sosa] = $birth; 3286ccdf4f0SGreg Roach } 3296ccdf4f0SGreg Roach } 3306ccdf4f0SGreg Roach } 3316ccdf4f0SGreg Roach 3326ccdf4f0SGreg Roach return $facts; 3331f374598SGreg Roach } 3341f374598SGreg Roach 3351f374598SGreg Roach /** 33631e26437SGreg Roach * builds and returns sosa relationship name in the active language 33731e26437SGreg Roach * 33831e26437SGreg Roach * @param int $sosa Sosa number 33931e26437SGreg Roach * 34031e26437SGreg Roach * @return string 34131e26437SGreg Roach */ 34231e26437SGreg Roach private function getSosaName(int $sosa): string 34331e26437SGreg Roach { 34431e26437SGreg Roach $path = ''; 34531e26437SGreg Roach 34631e26437SGreg Roach while ($sosa > 1) { 34731e26437SGreg Roach if ($sosa % 2 === 1) { 34831e26437SGreg Roach $path = 'mot' . $path; 34931e26437SGreg Roach } else { 35031e26437SGreg Roach $path = 'fat' . $path; 35131e26437SGreg Roach } 35231e26437SGreg Roach $sosa = intdiv($sosa, 2); 35331e26437SGreg Roach } 35431e26437SGreg Roach 3556fcafd02SGreg Roach return app(RelationshipService::class)->legacyNameAlgorithm($path); 35631e26437SGreg Roach } 3571f374598SGreg Roach} 358