11f374598SGreg Roach<?php 21f374598SGreg Roach/** 31f374598SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 51f374598SGreg Roach * This program is free software: you can redistribute it and/or modify 61f374598SGreg Roach * it under the terms of the GNU General Public License as published by 71f374598SGreg Roach * the Free Software Foundation, either version 3 of the License, or 81f374598SGreg Roach * (at your option) any later version. 91f374598SGreg Roach * This program is distributed in the hope that it will be useful, 101f374598SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 111f374598SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 121f374598SGreg Roach * GNU General Public License for more details. 131f374598SGreg Roach * You should have received a copy of the GNU General Public License 141f374598SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 151f374598SGreg Roach */ 161f374598SGreg Roachdeclare(strict_types=1); 171f374598SGreg Roach 181f374598SGreg Roachnamespace Fisharebest\Webtrees\Module; 191f374598SGreg Roach 201f374598SGreg Roachuse Exception; 21b6e50991SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException; 22b6e50991SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 231f374598SGreg Roachuse Fisharebest\Webtrees\Fact; 241f374598SGreg Roachuse Fisharebest\Webtrees\FactLocation; 251f374598SGreg Roachuse Fisharebest\Webtrees\I18N; 261f374598SGreg Roachuse Fisharebest\Webtrees\Individual; 271f374598SGreg Roachuse Fisharebest\Webtrees\Menu; 28aca28033SGreg Roachuse Fisharebest\Webtrees\Services\ChartService; 291f374598SGreg Roachuse Fisharebest\Webtrees\Tree; 308d0ebef0SGreg Roachuse Fisharebest\Webtrees\Webtrees; 311f374598SGreg Roachuse Symfony\Component\HttpFoundation\JsonResponse; 321f374598SGreg Roachuse Symfony\Component\HttpFoundation\Request; 337d988ec3SGreg Roachuse Symfony\Component\HttpFoundation\Response; 341f374598SGreg Roach 351f374598SGreg Roach/** 361f374598SGreg Roach * Class PedigreeMapModule 371f374598SGreg Roach */ 3837eb8894SGreg Roachclass PedigreeMapModule extends AbstractModule implements ModuleChartInterface 391f374598SGreg Roach{ 4049a243cbSGreg Roach use ModuleChartTrait; 4149a243cbSGreg Roach 4216d6367aSGreg Roach private const LINE_COLORS = [ 431f374598SGreg Roach '#FF0000', 441f374598SGreg Roach // Red 451f374598SGreg Roach '#00FF00', 461f374598SGreg Roach // Green 471f374598SGreg Roach '#0000FF', 481f374598SGreg Roach // Blue 491f374598SGreg Roach '#FFB300', 501f374598SGreg Roach // Gold 511f374598SGreg Roach '#00FFFF', 521f374598SGreg Roach // Cyan 531f374598SGreg Roach '#FF00FF', 541f374598SGreg Roach // Purple 551f374598SGreg Roach '#7777FF', 561f374598SGreg Roach // Light blue 571f374598SGreg Roach '#80FF80' 581f374598SGreg Roach // Light green 591f374598SGreg Roach ]; 601f374598SGreg Roach 611f374598SGreg Roach private static $map_providers = null; 621f374598SGreg Roach private static $map_selections = null; 631f374598SGreg Roach 64961ec755SGreg Roach /** 65961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 66961ec755SGreg Roach * 67961ec755SGreg Roach * @return string 68961ec755SGreg Roach */ 6949a243cbSGreg Roach public function title(): string 701f374598SGreg Roach { 71bbb76c12SGreg Roach /* I18N: Name of a module */ 72bbb76c12SGreg Roach return I18N::translate('Pedigree map'); 731f374598SGreg Roach } 741f374598SGreg Roach 75961ec755SGreg Roach /** 76961ec755SGreg Roach * A sentence describing what this module does. 77961ec755SGreg Roach * 78961ec755SGreg Roach * @return string 79961ec755SGreg Roach */ 8049a243cbSGreg Roach public function description(): string 811f374598SGreg Roach { 82bbb76c12SGreg Roach /* I18N: Description of the “OSM” module */ 83bbb76c12SGreg Roach return I18N::translate('Show the birthplace of ancestors on a map.'); 841f374598SGreg Roach } 851f374598SGreg Roach 861f374598SGreg Roach /** 87377a2979SGreg Roach * CSS class for the URL. 88377a2979SGreg Roach * 89377a2979SGreg Roach * @return string 90377a2979SGreg Roach */ 91377a2979SGreg Roach public function chartMenuClass(): string 92377a2979SGreg Roach { 93377a2979SGreg Roach return 'menu-chart-pedigreemap'; 94377a2979SGreg Roach } 95377a2979SGreg Roach 96377a2979SGreg Roach /** 971f374598SGreg Roach * Return a menu item for this chart - for use in individual boxes. 981f374598SGreg Roach * 991f374598SGreg Roach * @param Individual $individual 1001f374598SGreg Roach * 10149a243cbSGreg Roach * @return Menu|null 1021f374598SGreg Roach */ 103377a2979SGreg Roach public function chartBoxMenu(Individual $individual): ?Menu 1041f374598SGreg Roach { 105e6562982SGreg Roach return $this->chartMenu($individual); 106e6562982SGreg Roach } 107e6562982SGreg Roach 108e6562982SGreg Roach /** 109e6562982SGreg Roach * The title for a specific instance of this chart. 110e6562982SGreg Roach * 111e6562982SGreg Roach * @param Individual $individual 112e6562982SGreg Roach * 113e6562982SGreg Roach * @return string 114e6562982SGreg Roach */ 115e6562982SGreg Roach public function chartTitle(Individual $individual): string 116e6562982SGreg Roach { 117e6562982SGreg Roach /* I18N: %s is an individual’s name */ 118e6562982SGreg Roach return I18N::translate('Pedigree map of %s', $individual->getFullName()); 119e6562982SGreg Roach } 120e6562982SGreg Roach 121e6562982SGreg Roach /** 122e6562982SGreg Roach * The URL for this chart. 123e6562982SGreg Roach * 124e6562982SGreg Roach * @param Individual $individual 125e6562982SGreg Roach * @param string[] $parameters 126e6562982SGreg Roach * 127e6562982SGreg Roach * @return string 128e6562982SGreg Roach */ 129e6562982SGreg Roach public function chartUrl(Individual $individual, array $parameters = []): string 130e6562982SGreg Roach { 131e6562982SGreg Roach return route('module', [ 13226684e68SGreg Roach 'module' => $this->name(), 133e6562982SGreg Roach 'action' => 'PedigreeMap', 134e6562982SGreg Roach 'xref' => $individual->xref(), 135e6562982SGreg Roach 'ged' => $individual->tree()->name(), 136e6562982SGreg Roach ] + $parameters); 137e6562982SGreg Roach } 138e6562982SGreg Roach 139e6562982SGreg Roach /** 1401f374598SGreg Roach * @param Request $request 141b6db7c1fSGreg Roach * @param Tree $tree 142aca28033SGreg Roach * @param ChartService $chart_service 1431f374598SGreg Roach * 1441f374598SGreg Roach * @return JsonResponse 1451f374598SGreg Roach */ 146aca28033SGreg Roach public function getMapDataAction(Request $request, Tree $tree, ChartService $chart_service): JsonResponse 1471f374598SGreg Roach { 1481f374598SGreg Roach $xref = $request->get('reference'); 1491f374598SGreg Roach $indi = Individual::getInstance($xref, $tree); 1501f374598SGreg Roach $color_count = count(self::LINE_COLORS); 1511f374598SGreg Roach 152aca28033SGreg Roach $facts = $this->getPedigreeMapFacts($request, $tree, $chart_service); 1531f374598SGreg Roach 1541f374598SGreg Roach $geojson = [ 1551f374598SGreg Roach 'type' => 'FeatureCollection', 1561f374598SGreg Roach 'features' => [], 1571f374598SGreg Roach ]; 1587d988ec3SGreg Roach 1597d988ec3SGreg Roach $sosa_points = []; 1607d988ec3SGreg Roach 1611f374598SGreg Roach foreach ($facts as $id => $fact) { 1621f374598SGreg Roach $event = new FactLocation($fact, $indi); 1631f374598SGreg Roach $icon = $event->getIconDetails(); 1641f374598SGreg Roach if ($event->knownLatLon()) { 1651f374598SGreg Roach $polyline = null; 1661f374598SGreg Roach $color = self::LINE_COLORS[log($id, 2) % $color_count]; 1671f374598SGreg Roach $icon['color'] = $color; //make icon color the same as the line 1681f374598SGreg Roach $sosa_points[$id] = $event->getLatLonJSArray(); 169cdaafeeeSGreg Roach $sosa_parent = intdiv($id, 2); 1701f374598SGreg Roach if (array_key_exists($sosa_parent, $sosa_points)) { 1711f374598SGreg Roach // Would like to use a GeometryCollection to hold LineStrings 1721f374598SGreg Roach // rather than generate polylines but the MarkerCluster library 1731f374598SGreg Roach // doesn't seem to like them 1741f374598SGreg Roach $polyline = [ 1751f374598SGreg Roach 'points' => [ 1761f374598SGreg Roach $sosa_points[$sosa_parent], 1771f374598SGreg Roach $event->getLatLonJSArray(), 1781f374598SGreg Roach ], 1791f374598SGreg Roach 'options' => [ 1801f374598SGreg Roach 'color' => $color, 1811f374598SGreg Roach ], 1821f374598SGreg Roach ]; 1831f374598SGreg Roach } 1841f374598SGreg Roach $geojson['features'][] = [ 1851f374598SGreg Roach 'type' => 'Feature', 1861f374598SGreg Roach 'id' => $id, 1871f374598SGreg Roach 'valid' => true, 1881f374598SGreg Roach 'geometry' => [ 1891f374598SGreg Roach 'type' => 'Point', 1901f374598SGreg Roach 'coordinates' => $event->getGeoJsonCoords(), 1911f374598SGreg Roach ], 1921f374598SGreg Roach 'properties' => [ 1931f374598SGreg Roach 'polyline' => $polyline, 1941f374598SGreg Roach 'icon' => $icon, 1951f374598SGreg Roach 'tooltip' => $event->toolTip(), 1963dcc812bSGreg Roach 'summary' => view('modules/pedigree-map/events', $event->shortSummary('pedigree', $id)), 1971f374598SGreg Roach 'zoom' => (int) $event->getZoom(), 1981f374598SGreg Roach ], 1991f374598SGreg Roach ]; 2001f374598SGreg Roach } 2011f374598SGreg Roach } 2027d988ec3SGreg Roach 2037d988ec3SGreg Roach $code = empty($facts) ? Response::HTTP_NO_CONTENT : Response::HTTP_OK; 2041f374598SGreg Roach 2051f374598SGreg Roach return new JsonResponse($geojson, $code); 2061f374598SGreg Roach } 2071f374598SGreg Roach 2081f374598SGreg Roach /** 2091f374598SGreg Roach * @param Request $request 210b6db7c1fSGreg Roach * @param Tree $tree 211aca28033SGreg Roach * @param ChartService $chart_service 2121f374598SGreg Roach * 2131f374598SGreg Roach * @return array 2141f374598SGreg Roach */ 215aca28033SGreg Roach private function getPedigreeMapFacts(Request $request, Tree $tree, ChartService $chart_service): array 2161f374598SGreg Roach { 2171f374598SGreg Roach $xref = $request->get('reference'); 2181f374598SGreg Roach $individual = Individual::getInstance($xref, $tree); 2191f374598SGreg Roach $generations = (int) $request->get( 2201f374598SGreg Roach 'generations', 2211f374598SGreg Roach $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS') 2221f374598SGreg Roach ); 223aca28033SGreg Roach $ancestors = $chart_service->sosaStradonitzAncestors($individual, $generations); 2241f374598SGreg Roach $facts = []; 2251f374598SGreg Roach foreach ($ancestors as $sosa => $person) { 226aca28033SGreg Roach if ($person->canShow()) { 2271f374598SGreg Roach $birth = $person->getFirstFact('BIRT'); 228*392561bbSGreg Roach if ($birth instanceof Fact && $birth->place()->gedcomName() !== '') { 2291f374598SGreg Roach $facts[$sosa] = $birth; 2301f374598SGreg Roach } 2311f374598SGreg Roach } 2321f374598SGreg Roach } 2331f374598SGreg Roach 2341f374598SGreg Roach return $facts; 2351f374598SGreg Roach } 2361f374598SGreg Roach 2371f374598SGreg Roach /** 2381f374598SGreg Roach * @param Request $request 2391f374598SGreg Roach * 2401f374598SGreg Roach * @return JsonResponse 2411f374598SGreg Roach */ 2421f374598SGreg Roach public function getProviderStylesAction(Request $request): JsonResponse 2431f374598SGreg Roach { 2441f374598SGreg Roach $styles = $this->getMapProviderData($request); 2451f374598SGreg Roach 2461f374598SGreg Roach return new JsonResponse($styles); 2471f374598SGreg Roach } 2481f374598SGreg Roach 2491f374598SGreg Roach /** 2501f374598SGreg Roach * @param Request $request 2511f374598SGreg Roach * 2521f374598SGreg Roach * @return array|null 2531f374598SGreg Roach */ 2541f374598SGreg Roach private function getMapProviderData(Request $request) 2551f374598SGreg Roach { 2561f374598SGreg Roach if (self::$map_providers === null) { 2578d0ebef0SGreg Roach $providersFile = WT_ROOT . Webtrees::MODULES_PATH . 'openstreetmap/providers/providers.xml'; 2581f374598SGreg Roach self::$map_selections = [ 2591f374598SGreg Roach 'provider' => $this->getPreference('provider', 'openstreetmap'), 2601f374598SGreg Roach 'style' => $this->getPreference('provider_style', 'mapnik'), 2611f374598SGreg Roach ]; 2621f374598SGreg Roach 2631f374598SGreg Roach try { 2641f374598SGreg Roach $xml = simplexml_load_file($providersFile); 2651f374598SGreg Roach // need to convert xml structure into arrays & strings 2661f374598SGreg Roach foreach ($xml as $provider) { 2671f374598SGreg Roach $style_keys = array_map( 26818d7a90dSGreg Roach function (string $item): string { 2691f374598SGreg Roach return preg_replace('/[^a-z\d]/i', '', strtolower($item)); 2701f374598SGreg Roach }, 2711f374598SGreg Roach (array) $provider->styles 2721f374598SGreg Roach ); 2731f374598SGreg Roach 2741f374598SGreg Roach $key = preg_replace('/[^a-z\d]/i', '', strtolower((string) $provider->name)); 2751f374598SGreg Roach 2761f374598SGreg Roach self::$map_providers[$key] = [ 2771f374598SGreg Roach 'name' => (string) $provider->name, 2781f374598SGreg Roach 'styles' => array_combine($style_keys, (array) $provider->styles), 2791f374598SGreg Roach ]; 2801f374598SGreg Roach } 2811f374598SGreg Roach } catch (Exception $ex) { 2821f374598SGreg Roach // Default provider is OpenStreetMap 2831f374598SGreg Roach self::$map_selections = [ 2841f374598SGreg Roach 'provider' => 'openstreetmap', 2851f374598SGreg Roach 'style' => 'mapnik', 2861f374598SGreg Roach ]; 2871f374598SGreg Roach self::$map_providers = [ 2881f374598SGreg Roach 'openstreetmap' => [ 2891f374598SGreg Roach 'name' => 'OpenStreetMap', 2901f374598SGreg Roach 'styles' => ['mapnik' => 'Mapnik'], 2911f374598SGreg Roach ], 2921f374598SGreg Roach ]; 2931f374598SGreg Roach }; 2941f374598SGreg Roach } 2951f374598SGreg Roach 2961f374598SGreg Roach //Ugly!!! 2971f374598SGreg Roach switch ($request->get('action')) { 2981f374598SGreg Roach case 'BaseData': 2991f374598SGreg Roach $varName = (self::$map_selections['style'] === '') ? '' : self::$map_providers[self::$map_selections['provider']]['styles'][self::$map_selections['style']]; 3001f374598SGreg Roach $payload = [ 3011f374598SGreg Roach 'selectedProvIndex' => self::$map_selections['provider'], 3021f374598SGreg Roach 'selectedProvName' => self::$map_providers[self::$map_selections['provider']]['name'], 3031f374598SGreg Roach 'selectedStyleName' => $varName, 3041f374598SGreg Roach ]; 3051f374598SGreg Roach break; 3061f374598SGreg Roach case 'ProviderStyles': 3071f374598SGreg Roach $provider = $request->get('provider', 'openstreetmap'); 3081f374598SGreg Roach $payload = self::$map_providers[$provider]['styles']; 3091f374598SGreg Roach break; 3101f374598SGreg Roach case 'AdminConfig': 3111f374598SGreg Roach $providers = []; 3121f374598SGreg Roach foreach (self::$map_providers as $key => $provider) { 3131f374598SGreg Roach $providers[$key] = $provider['name']; 3141f374598SGreg Roach } 3151f374598SGreg Roach $payload = [ 3161f374598SGreg Roach 'providers' => $providers, 3171f374598SGreg Roach 'selectedProv' => self::$map_selections['provider'], 3181f374598SGreg Roach 'styles' => self::$map_providers[self::$map_selections['provider']]['styles'], 3191f374598SGreg Roach 'selectedStyle' => self::$map_selections['style'], 3201f374598SGreg Roach ]; 3211f374598SGreg Roach break; 3221f374598SGreg Roach default: 3231f374598SGreg Roach $payload = null; 3241f374598SGreg Roach } 3251f374598SGreg Roach 3261f374598SGreg Roach return $payload; 3271f374598SGreg Roach } 3281f374598SGreg Roach 3291f374598SGreg Roach /** 3301f374598SGreg Roach * @param Request $request 331b6db7c1fSGreg Roach * @param Tree $tree 3321f374598SGreg Roach * 3331f374598SGreg Roach * @return object 3341f374598SGreg Roach */ 335b6db7c1fSGreg Roach public function getPedigreeMapAction(Request $request, Tree $tree) 3361f374598SGreg Roach { 33747b1b9caSGreg Roach $xref = $request->get('xref', ''); 3381f374598SGreg Roach $individual = Individual::getInstance($xref, $tree); 3391f374598SGreg Roach $maxgenerations = $tree->getPreference('MAX_PEDIGREE_GENERATIONS'); 3401f374598SGreg Roach $generations = $request->get('generations', $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS')); 3411f374598SGreg Roach 342b6e50991SGreg Roach if ($individual === null) { 34359f2f229SGreg Roach throw new IndividualNotFoundException(); 344b2ce94c6SRico Sonntag } 345b2ce94c6SRico Sonntag 346b2ce94c6SRico Sonntag if (!$individual->canShow()) { 34759f2f229SGreg Roach throw new IndividualAccessDeniedException(); 348b6e50991SGreg Roach } 349b6e50991SGreg Roach 3503dcc812bSGreg Roach return $this->viewResponse('modules/pedigree-map/page', [ 3513dcc812bSGreg Roach 'module_name' => $this->name(), 352bbb76c12SGreg Roach /* I18N: %s is an individual’s name */ 353bbb76c12SGreg Roach 'title' => I18N::translate('Pedigree map of %s', $individual->getFullName()), 3541f374598SGreg Roach 'tree' => $tree, 3551f374598SGreg Roach 'individual' => $individual, 3561f374598SGreg Roach 'generations' => $generations, 3571f374598SGreg Roach 'maxgenerations' => $maxgenerations, 3581f374598SGreg Roach 'map' => view( 3593dcc812bSGreg Roach 'modules/pedigree-map/chart', 3601f374598SGreg Roach [ 36126684e68SGreg Roach 'module' => $this->name(), 362c0935879SGreg Roach 'ref' => $individual->xref(), 3631f374598SGreg Roach 'type' => 'pedigree', 3641f374598SGreg Roach 'generations' => $generations, 3651f374598SGreg Roach ] 366e6562982SGreg Roach ), 367aca28033SGreg Roach ]); 3681f374598SGreg Roach } 3691f374598SGreg Roach} 370