11f374598SGreg Roach<?php 21f374598SGreg Roach/** 31f374598SGreg Roach * webtrees: online genealogy 41f374598SGreg Roach * Copyright (C) 2018 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; 211f374598SGreg Roachuse Fisharebest\Webtrees\Auth; 221f374598SGreg Roachuse Fisharebest\Webtrees\Database; 231f374598SGreg Roachuse Fisharebest\Webtrees\Fact; 241f374598SGreg Roachuse Fisharebest\Webtrees\FactLocation; 251f374598SGreg Roachuse Fisharebest\Webtrees\Functions\Functions; 261f374598SGreg Roachuse Fisharebest\Webtrees\I18N; 271f374598SGreg Roachuse Fisharebest\Webtrees\Individual; 281f374598SGreg Roachuse Symfony\Component\HttpFoundation\JsonResponse; 291f374598SGreg Roachuse Symfony\Component\HttpFoundation\Request; 301f374598SGreg Roach 311f374598SGreg Roach/** 321f374598SGreg Roach * Class PlacesMapModule 331f374598SGreg Roach */ 341f374598SGreg Roachclass PlacesModule extends AbstractModule implements ModuleTabInterface 351f374598SGreg Roach{ 361f374598SGreg Roach const OSM_MIN_ZOOM = 2; 371f374598SGreg Roach const LINE_COLORS = [ 381f374598SGreg Roach '#FF0000', 391f374598SGreg Roach // Red 401f374598SGreg Roach '#00FF00', 411f374598SGreg Roach // Green 421f374598SGreg Roach '#0000FF', 431f374598SGreg Roach // Blue 441f374598SGreg Roach '#FFB300', 451f374598SGreg Roach // Gold 461f374598SGreg Roach '#00FFFF', 471f374598SGreg Roach // Cyan 481f374598SGreg Roach '#FF00FF', 491f374598SGreg Roach // Purple 501f374598SGreg Roach '#7777FF', 511f374598SGreg Roach // Light blue 521f374598SGreg Roach '#80FF80' 531f374598SGreg Roach // Light green 541f374598SGreg Roach ]; 551f374598SGreg Roach 561f374598SGreg Roach private static $map_providers = null; 571f374598SGreg Roach private static $map_selections = null; 581f374598SGreg Roach 591f374598SGreg Roach /** {@inheritdoc} */ 601f374598SGreg Roach public function getTitle() 611f374598SGreg Roach { 621f374598SGreg Roach return /* I18N: Name of a module */ 631f374598SGreg Roach I18N::translate('Places'); 641f374598SGreg Roach } 651f374598SGreg Roach 661f374598SGreg Roach /** {@inheritdoc} */ 671f374598SGreg Roach public function getDescription() 681f374598SGreg Roach { 691f374598SGreg Roach return /* I18N: Description of the “OSM” module */ 701f374598SGreg Roach I18N::translate('Show the location of events on a map.'); 711f374598SGreg Roach } 721f374598SGreg Roach 731f374598SGreg Roach /** {@inheritdoc} */ 741f374598SGreg Roach public function defaultAccessLevel() 751f374598SGreg Roach { 761f374598SGreg Roach return Auth::PRIV_PRIVATE; 771f374598SGreg Roach } 781f374598SGreg Roach 791f374598SGreg Roach /** {@inheritdoc} */ 801f374598SGreg Roach public function defaultTabOrder() 811f374598SGreg Roach { 821f374598SGreg Roach return 4; 831f374598SGreg Roach } 841f374598SGreg Roach 851f374598SGreg Roach /** {@inheritdoc} */ 861f374598SGreg Roach public function hasTabContent(Individual $individual) 871f374598SGreg Roach { 881f374598SGreg Roach return true; 891f374598SGreg Roach } 901f374598SGreg Roach 911f374598SGreg Roach /** {@inheritdoc} */ 921f374598SGreg Roach public function isGrayedOut(Individual $individual) 931f374598SGreg Roach { 941f374598SGreg Roach return false; 951f374598SGreg Roach } 961f374598SGreg Roach 971f374598SGreg Roach /** {@inheritdoc} */ 981f374598SGreg Roach public function canLoadAjax() 991f374598SGreg Roach { 1001f374598SGreg Roach return true; 1011f374598SGreg Roach } 1021f374598SGreg Roach 1031f374598SGreg Roach /** {@inheritdoc} */ 1041f374598SGreg Roach public function getTabContent(Individual $individual) 1051f374598SGreg Roach { 106aacdcb0dSGreg Roach return view('modules/places/tab', [ 1071f374598SGreg Roach 'module' => $this->getName(), 1081f374598SGreg Roach 'ref' => $individual->getXref(), 1091f374598SGreg Roach 'type' => 'individual', 110aacdcb0dSGreg Roach ]); 1111f374598SGreg Roach } 1121f374598SGreg Roach 1131f374598SGreg Roach /** 1141f374598SGreg Roach * @param Request $request 1151f374598SGreg Roach * 1161f374598SGreg Roach * @return JsonResponse 1171f374598SGreg Roach */ 1181f374598SGreg Roach public function getBaseDataAction(Request $request): JsonResponse 1191f374598SGreg Roach { 1201f374598SGreg Roach $provider = $this->getMapProviderData($request); 1211f374598SGreg Roach $style = $provider['selectedStyleName'] = '' ? '' : '.' . $provider['selectedStyleName']; 1221f374598SGreg Roach 1231f374598SGreg Roach switch ($provider['selectedProvIndex']) { 1241f374598SGreg Roach case 'mapbox': 1251f374598SGreg Roach $providerOptions = [ 1261f374598SGreg Roach 'id' => $this->getPreference('mapbox_id'), 1271f374598SGreg Roach 'accessToken' => $this->getPreference('mapbox_token'), 1281f374598SGreg Roach ]; 1291f374598SGreg Roach break; 1301f374598SGreg Roach case 'here': 1311f374598SGreg Roach $providerOptions = [ 1321f374598SGreg Roach 'app_id' => $this->getPreference('here_appid'), 1331f374598SGreg Roach 'app_code' => $this->getPreference('here_appcode'), 1341f374598SGreg Roach ]; 1351f374598SGreg Roach break; 1361f374598SGreg Roach default: 1371f374598SGreg Roach $providerOptions = []; 1381f374598SGreg Roach }; 1391f374598SGreg Roach 1401f374598SGreg Roach $options = [ 1411f374598SGreg Roach 'minZoom' => self::OSM_MIN_ZOOM, 1421f374598SGreg Roach 'providerName' => $provider['selectedProvName'] . $style, 1431f374598SGreg Roach 'providerOptions' => $providerOptions, 1441f374598SGreg Roach 'animate' => $this->getPreference('map_animate', 0), 1451f374598SGreg Roach 'I18N' => [ 1461f374598SGreg Roach 'zoomInTitle' => I18N::translate('Zoom in'), 1471f374598SGreg Roach 'zoomOutTitle' => I18N::translate('Zoom out'), 1481f374598SGreg Roach 'reset' => I18N::translate('Reset to initial map state'), 1491f374598SGreg Roach 'noData' => I18N::translate('No mappable items'), 1501f374598SGreg Roach 'error' => I18N::translate('An unknown error occurred'), 1511f374598SGreg Roach ], 1521f374598SGreg Roach ]; 1531f374598SGreg Roach 1541f374598SGreg Roach return new JsonResponse($options); 1551f374598SGreg Roach } 1561f374598SGreg Roach 1571f374598SGreg Roach /** 1581f374598SGreg Roach * @param Request $request 1591f374598SGreg Roach * 1601f374598SGreg Roach * @return JsonResponse 1611f374598SGreg Roach * @throws Exception 1621f374598SGreg Roach */ 1631f374598SGreg Roach public function getMapDataAction(Request $request): JsonResponse 1641f374598SGreg Roach { 1651f374598SGreg Roach $xref = $request->get('reference'); 1661f374598SGreg Roach $tree = $request->attributes->get('tree'); 1671f374598SGreg Roach $indi = Individual::getInstance($xref, $tree); 1681f374598SGreg Roach $facts = $this->getPersonalFacts($request); 1691f374598SGreg Roach 1701f374598SGreg Roach $geojson = [ 1711f374598SGreg Roach 'type' => 'FeatureCollection', 1721f374598SGreg Roach 'features' => [], 1731f374598SGreg Roach ]; 1741f374598SGreg Roach if (empty($facts)) { 1751f374598SGreg Roach $code = 204; 1761f374598SGreg Roach } else { 1771f374598SGreg Roach $code = 200; 1781f374598SGreg Roach foreach ($facts as $id => $fact) { 1791f374598SGreg Roach $event = new FactLocation($fact, $indi); 1801f374598SGreg Roach $icon = $event->getIconDetails(); 1811f374598SGreg Roach if ($event->knownLatLon()) { 1821f374598SGreg Roach $polyline = null; 1831f374598SGreg Roach $geojson['features'][] = [ 1841f374598SGreg Roach 'type' => 'Feature', 1851f374598SGreg Roach 'id' => $id, 1861f374598SGreg Roach 'valid' => true, 1871f374598SGreg Roach 'geometry' => [ 1881f374598SGreg Roach 'type' => 'Point', 1891f374598SGreg Roach 'coordinates' => $event->getGeoJsonCoords(), 1901f374598SGreg Roach ], 1911f374598SGreg Roach 'properties' => [ 1921f374598SGreg Roach 'polyline' => $polyline, 1931f374598SGreg Roach 'icon' => $icon, 1941f374598SGreg Roach 'tooltip' => $event->toolTip(), 1951f374598SGreg Roach 'summary' => view( 196*0a661b58SGreg Roach 'modules/places/event-sidebar', 1971f374598SGreg Roach $event->shortSummary('individual', $id) 1981f374598SGreg Roach ), 1991f374598SGreg Roach 'zoom' => (int) $event->getZoom(), 2001f374598SGreg Roach ], 2011f374598SGreg Roach ]; 2021f374598SGreg Roach } 2031f374598SGreg Roach } 2041f374598SGreg Roach } 2051f374598SGreg Roach 2061f374598SGreg Roach return new JsonResponse($geojson, $code); 2071f374598SGreg Roach } 2081f374598SGreg Roach 2091f374598SGreg Roach /** 2101f374598SGreg Roach * @param Request $request 2111f374598SGreg Roach * 2121f374598SGreg Roach * @return array 2131f374598SGreg Roach * @throws Exception 2141f374598SGreg Roach */ 2151f374598SGreg Roach private function getPersonalFacts(Request $request) 2161f374598SGreg Roach { 2171f374598SGreg Roach $xref = $request->get('reference'); 2181f374598SGreg Roach $tree = $request->attributes->get('tree'); 2191f374598SGreg Roach $individual = Individual::getInstance($xref, $tree); 2201f374598SGreg Roach $facts = $individual->getFacts(); 2211f374598SGreg Roach foreach ($individual->getSpouseFamilies() as $family) { 2221f374598SGreg Roach $facts = array_merge($facts, $family->getFacts()); 2231f374598SGreg Roach // Add birth of children from this family to the facts array 2241f374598SGreg Roach foreach ($family->getChildren() as $child) { 2251f374598SGreg Roach $childsBirth = $child->getFirstFact('BIRT'); 2261f374598SGreg Roach if ($childsBirth && !$childsBirth->getPlace()->isEmpty()) { 2271f374598SGreg Roach $facts[] = $childsBirth; 2281f374598SGreg Roach } 2291f374598SGreg Roach } 2301f374598SGreg Roach } 2311f374598SGreg Roach 2321f374598SGreg Roach Functions::sortFacts($facts); 2331f374598SGreg Roach 2341f374598SGreg Roach $useable_facts = array_filter( 2351f374598SGreg Roach $facts, 2361f374598SGreg Roach function (Fact $item) { 2371f374598SGreg Roach return !$item->getPlace()->isEmpty(); 2381f374598SGreg Roach } 2391f374598SGreg Roach ); 2401f374598SGreg Roach 2411f374598SGreg Roach return array_values($useable_facts); 2421f374598SGreg Roach } 2431f374598SGreg Roach 2441f374598SGreg Roach /** 2451f374598SGreg Roach * @param Request $request 2461f374598SGreg Roach * 2471f374598SGreg Roach * @return JsonResponse 2481f374598SGreg Roach */ 2491f374598SGreg Roach public function getProviderStylesAction(Request $request): JsonResponse 2501f374598SGreg Roach { 2511f374598SGreg Roach $styles = $this->getMapProviderData($request); 2521f374598SGreg Roach 2531f374598SGreg Roach return new JsonResponse($styles); 2541f374598SGreg Roach } 2551f374598SGreg Roach 2561f374598SGreg Roach /** 2571f374598SGreg Roach * @param Request $request 2581f374598SGreg Roach * 2591f374598SGreg Roach * @return array|null 2601f374598SGreg Roach */ 2611f374598SGreg Roach private function getMapProviderData(Request $request) 2621f374598SGreg Roach { 2631f374598SGreg Roach if (self::$map_providers === null) { 264*0a661b58SGreg Roach $providersFile = WT_ROOT . WT_MODULES_DIR . 'openstreetmap/providers/providers.xml'; 2651f374598SGreg Roach self::$map_selections = [ 2661f374598SGreg Roach 'provider' => $this->getPreference('provider', 'openstreetmap'), 2671f374598SGreg Roach 'style' => $this->getPreference('provider_style', 'mapnik'), 2681f374598SGreg Roach ]; 2691f374598SGreg Roach 2701f374598SGreg Roach try { 2711f374598SGreg Roach $xml = simplexml_load_file($providersFile); 2721f374598SGreg Roach // need to convert xml structure into arrays & strings 2731f374598SGreg Roach foreach ($xml as $provider) { 2741f374598SGreg Roach $style_keys = array_map( 2751f374598SGreg Roach function ($item) { 2761f374598SGreg Roach return preg_replace('/[^a-z\d]/i', '', strtolower($item)); 2771f374598SGreg Roach }, 2781f374598SGreg Roach (array) $provider->styles 2791f374598SGreg Roach ); 2801f374598SGreg Roach 2811f374598SGreg Roach $key = preg_replace('/[^a-z\d]/i', '', strtolower((string) $provider->name)); 2821f374598SGreg Roach 2831f374598SGreg Roach self::$map_providers[$key] = [ 2841f374598SGreg Roach 'name' => (string) $provider->name, 2851f374598SGreg Roach 'styles' => array_combine($style_keys, (array) $provider->styles), 2861f374598SGreg Roach ]; 2871f374598SGreg Roach } 2881f374598SGreg Roach } catch (Exception $ex) { 2891f374598SGreg Roach // Default provider is OpenStreetMap 2901f374598SGreg Roach self::$map_selections = [ 2911f374598SGreg Roach 'provider' => 'openstreetmap', 2921f374598SGreg Roach 'style' => 'mapnik', 2931f374598SGreg Roach ]; 2941f374598SGreg Roach self::$map_providers = [ 2951f374598SGreg Roach 'openstreetmap' => [ 2961f374598SGreg Roach 'name' => 'OpenStreetMap', 2971f374598SGreg Roach 'styles' => ['mapnik' => 'Mapnik'], 2981f374598SGreg Roach ], 2991f374598SGreg Roach ]; 3001f374598SGreg Roach }; 3011f374598SGreg Roach } 3021f374598SGreg Roach 3031f374598SGreg Roach //Ugly!!! 3041f374598SGreg Roach switch ($request->get('action')) { 3051f374598SGreg Roach case 'BaseData': 3061f374598SGreg Roach $varName = (self::$map_selections['style'] === '') ? '' : self::$map_providers[self::$map_selections['provider']]['styles'][self::$map_selections['style']]; 3071f374598SGreg Roach $payload = [ 3081f374598SGreg Roach 'selectedProvIndex' => self::$map_selections['provider'], 3091f374598SGreg Roach 'selectedProvName' => self::$map_providers[self::$map_selections['provider']]['name'], 3101f374598SGreg Roach 'selectedStyleName' => $varName, 3111f374598SGreg Roach ]; 3121f374598SGreg Roach break; 3131f374598SGreg Roach case 'ProviderStyles': 3141f374598SGreg Roach $provider = $request->get('provider', 'openstreetmap'); 3151f374598SGreg Roach $payload = self::$map_providers[$provider]['styles']; 3161f374598SGreg Roach break; 3171f374598SGreg Roach case 'AdminConfig': 3181f374598SGreg Roach $providers = []; 3191f374598SGreg Roach foreach (self::$map_providers as $key => $provider) { 3201f374598SGreg Roach $providers[$key] = $provider['name']; 3211f374598SGreg Roach } 3221f374598SGreg Roach $payload = [ 3231f374598SGreg Roach 'providers' => $providers, 3241f374598SGreg Roach 'selectedProv' => self::$map_selections['provider'], 3251f374598SGreg Roach 'styles' => self::$map_providers[self::$map_selections['provider']]['styles'], 3261f374598SGreg Roach 'selectedStyle' => self::$map_selections['style'], 3271f374598SGreg Roach ]; 3281f374598SGreg Roach break; 3291f374598SGreg Roach default: 3301f374598SGreg Roach $payload = null; 3311f374598SGreg Roach } 3321f374598SGreg Roach 3331f374598SGreg Roach return $payload; 3341f374598SGreg Roach } 3351f374598SGreg Roach 3361f374598SGreg Roach /** 3371f374598SGreg Roach * @param $parent_id 3381f374598SGreg Roach * @param $placename 3391f374598SGreg Roach * @param $places 3401f374598SGreg Roach * 3411f374598SGreg Roach * @throws Exception 3421f374598SGreg Roach */ 3431f374598SGreg Roach private function buildLevel($parent_id, $placename, &$places) 3441f374598SGreg Roach { 3451f374598SGreg Roach $level = array_search('', $placename); 3461f374598SGreg Roach $rows = (array) Database::prepare( 3471f374598SGreg Roach "SELECT pl_level, pl_id, pl_place, pl_long, pl_lati, pl_zoom, pl_icon FROM `##placelocation` WHERE pl_parent_id=? ORDER BY pl_place" 3481f374598SGreg Roach ) 3491f374598SGreg Roach ->execute([$parent_id]) 3501f374598SGreg Roach ->fetchAll(\PDO::FETCH_ASSOC); 3511f374598SGreg Roach 3521f374598SGreg Roach if (!empty($rows)) { 3531f374598SGreg Roach foreach ($rows as $row) { 3541f374598SGreg Roach $index = $row['pl_id']; 3551f374598SGreg Roach $placename[$level] = $row['pl_place']; 3561f374598SGreg Roach $places[] = array_merge([$row['pl_level']], $placename, array_splice($row, 3)); 3571f374598SGreg Roach $this->buildLevel($index, $placename, $places); 3581f374598SGreg Roach } 3591f374598SGreg Roach } 3601f374598SGreg Roach } 3611f374598SGreg Roach} 362