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; 211f374598SGreg Roachuse Fisharebest\Webtrees\Fact; 221f374598SGreg Roachuse Fisharebest\Webtrees\FactLocation; 231f374598SGreg Roachuse Fisharebest\Webtrees\Functions\Functions; 241f374598SGreg Roachuse Fisharebest\Webtrees\I18N; 251f374598SGreg Roachuse Fisharebest\Webtrees\Individual; 268d0ebef0SGreg Roachuse Fisharebest\Webtrees\Webtrees; 2705ff554cSGreg Roachuse stdClass; 281f374598SGreg Roachuse Symfony\Component\HttpFoundation\JsonResponse; 291f374598SGreg Roachuse Symfony\Component\HttpFoundation\Request; 301f374598SGreg Roach 311f374598SGreg Roach/** 321f374598SGreg Roach * Class PlacesMapModule 331f374598SGreg Roach */ 3449a243cbSGreg Roachclass PlacesModule extends AbstractModule implements ModuleInterface, ModuleTabInterface 351f374598SGreg Roach{ 3649a243cbSGreg Roach use ModuleTabTrait; 3749a243cbSGreg Roach 381f374598SGreg Roach private static $map_providers = null; 391f374598SGreg Roach private static $map_selections = null; 401f374598SGreg Roach 41*961ec755SGreg Roach /** 42*961ec755SGreg Roach * How should this module be labelled on tabs, menus, etc.? 43*961ec755SGreg Roach * 44*961ec755SGreg Roach * @return string 45*961ec755SGreg Roach */ 4649a243cbSGreg Roach public function title(): string 471f374598SGreg Roach { 48bbb76c12SGreg Roach /* I18N: Name of a module */ 49bbb76c12SGreg Roach return I18N::translate('Places'); 501f374598SGreg Roach } 511f374598SGreg Roach 52*961ec755SGreg Roach /** 53*961ec755SGreg Roach * A sentence describing what this module does. 54*961ec755SGreg Roach * 55*961ec755SGreg Roach * @return string 56*961ec755SGreg Roach */ 5749a243cbSGreg Roach public function description(): string 581f374598SGreg Roach { 59bbb76c12SGreg Roach /* I18N: Description of the “OSM” module */ 60bbb76c12SGreg Roach return I18N::translate('Show the location of events on a map.'); 611f374598SGreg Roach } 621f374598SGreg Roach 6349a243cbSGreg Roach /** 6449a243cbSGreg Roach * The default position for this tab. It can be changed in the control panel. 6549a243cbSGreg Roach * 6649a243cbSGreg Roach * @return int 6749a243cbSGreg Roach */ 68cbf4b7faSGreg Roach public function defaultTabOrder(): int 69cbf4b7faSGreg Roach { 7049a243cbSGreg Roach return 10; 711f374598SGreg Roach } 721f374598SGreg Roach 731f374598SGreg Roach /** {@inheritdoc} */ 748f53f488SRico Sonntag public function hasTabContent(Individual $individual): bool 751f374598SGreg Roach { 761f374598SGreg Roach return true; 771f374598SGreg Roach } 781f374598SGreg Roach 791f374598SGreg Roach /** {@inheritdoc} */ 808f53f488SRico Sonntag public function isGrayedOut(Individual $individual): bool 811f374598SGreg Roach { 821f374598SGreg Roach return false; 831f374598SGreg Roach } 841f374598SGreg Roach 851f374598SGreg Roach /** {@inheritdoc} */ 868f53f488SRico Sonntag public function canLoadAjax(): bool 871f374598SGreg Roach { 881f374598SGreg Roach return true; 891f374598SGreg Roach } 901f374598SGreg Roach 911f374598SGreg Roach /** {@inheritdoc} */ 929b34404bSGreg Roach public function getTabContent(Individual $individual): string 931f374598SGreg Roach { 94aacdcb0dSGreg Roach return view('modules/places/tab', [ 9505ff554cSGreg Roach 'data' => $this->getMapData($individual), 96aacdcb0dSGreg Roach ]); 971f374598SGreg Roach } 981f374598SGreg Roach 991f374598SGreg Roach /** 100e93111adSRico Sonntag * @param Individual $indi 1011f374598SGreg Roach * 10205ff554cSGreg Roach * @return stdClass 1031f374598SGreg Roach */ 10405ff554cSGreg Roach private function getMapData(Individual $indi): stdClass 1051f374598SGreg Roach { 10605ff554cSGreg Roach $facts = $this->getPersonalFacts($indi); 1071f374598SGreg Roach 1081f374598SGreg Roach $geojson = [ 1091f374598SGreg Roach 'type' => 'FeatureCollection', 1101f374598SGreg Roach 'features' => [], 1111f374598SGreg Roach ]; 11205ff554cSGreg Roach 1131f374598SGreg Roach foreach ($facts as $id => $fact) { 1141f374598SGreg Roach $event = new FactLocation($fact, $indi); 1151f374598SGreg Roach $icon = $event->getIconDetails(); 1161f374598SGreg Roach if ($event->knownLatLon()) { 1171f374598SGreg Roach $geojson['features'][] = [ 1181f374598SGreg Roach 'type' => 'Feature', 1191f374598SGreg Roach 'id' => $id, 1201f374598SGreg Roach 'valid' => true, 1211f374598SGreg Roach 'geometry' => [ 1221f374598SGreg Roach 'type' => 'Point', 1231f374598SGreg Roach 'coordinates' => $event->getGeoJsonCoords(), 1241f374598SGreg Roach ], 1251f374598SGreg Roach 'properties' => [ 12605ff554cSGreg Roach 'polyline' => null, 1271f374598SGreg Roach 'icon' => $icon, 1281f374598SGreg Roach 'tooltip' => $event->toolTip(), 1291f374598SGreg Roach 'summary' => view( 1300a661b58SGreg Roach 'modules/places/event-sidebar', 1311f374598SGreg Roach $event->shortSummary('individual', $id) 1321f374598SGreg Roach ), 1331f374598SGreg Roach 'zoom' => (int) $event->getZoom(), 1341f374598SGreg Roach ], 1351f374598SGreg Roach ]; 1361f374598SGreg Roach } 1371f374598SGreg Roach } 1381f374598SGreg Roach 13905ff554cSGreg Roach return (object) $geojson; 1401f374598SGreg Roach } 1411f374598SGreg Roach 1421f374598SGreg Roach /** 14305ff554cSGreg Roach * @param Individual $individual 1441f374598SGreg Roach * 1451f374598SGreg Roach * @return array 1461f374598SGreg Roach * @throws Exception 1471f374598SGreg Roach */ 1488f53f488SRico Sonntag private function getPersonalFacts(Individual $individual): array 1491f374598SGreg Roach { 15030158ae7SGreg Roach $facts = $individual->facts(); 1511f374598SGreg Roach foreach ($individual->getSpouseFamilies() as $family) { 15230158ae7SGreg Roach $facts = array_merge($facts, $family->facts()); 1531f374598SGreg Roach // Add birth of children from this family to the facts array 1541f374598SGreg Roach foreach ($family->getChildren() as $child) { 1551f374598SGreg Roach $childsBirth = $child->getFirstFact('BIRT'); 1564fb14fcbSGreg Roach if ($childsBirth && !$childsBirth->place()->isEmpty()) { 1571f374598SGreg Roach $facts[] = $childsBirth; 1581f374598SGreg Roach } 1591f374598SGreg Roach } 1601f374598SGreg Roach } 1611f374598SGreg Roach 1621f374598SGreg Roach Functions::sortFacts($facts); 1631f374598SGreg Roach 1641f374598SGreg Roach $useable_facts = array_filter( 1651f374598SGreg Roach $facts, 166492c7072SGreg Roach function (Fact $item): bool { 1674fb14fcbSGreg Roach return !$item->place()->isEmpty(); 1681f374598SGreg Roach } 1691f374598SGreg Roach ); 1701f374598SGreg Roach 1711f374598SGreg Roach return array_values($useable_facts); 1721f374598SGreg Roach } 1731f374598SGreg Roach 1741f374598SGreg Roach /** 1751f374598SGreg Roach * @param Request $request 1761f374598SGreg Roach * 1771f374598SGreg Roach * @return JsonResponse 1781f374598SGreg Roach */ 1791f374598SGreg Roach public function getProviderStylesAction(Request $request): JsonResponse 1801f374598SGreg Roach { 1811f374598SGreg Roach $styles = $this->getMapProviderData($request); 1821f374598SGreg Roach 1831f374598SGreg Roach return new JsonResponse($styles); 1841f374598SGreg Roach } 1851f374598SGreg Roach 1861f374598SGreg Roach /** 1871f374598SGreg Roach * @param Request $request 1881f374598SGreg Roach * 1891f374598SGreg Roach * @return array|null 1901f374598SGreg Roach */ 1911f374598SGreg Roach private function getMapProviderData(Request $request) 1921f374598SGreg Roach { 1931f374598SGreg Roach if (self::$map_providers === null) { 1948d0ebef0SGreg Roach $providersFile = WT_ROOT . Webtrees::MODULES_PATH . 'openstreetmap/providers/providers.xml'; 1951f374598SGreg Roach self::$map_selections = [ 1961f374598SGreg Roach 'provider' => $this->getPreference('provider', 'openstreetmap'), 1971f374598SGreg Roach 'style' => $this->getPreference('provider_style', 'mapnik'), 1981f374598SGreg Roach ]; 1991f374598SGreg Roach 2001f374598SGreg Roach try { 2011f374598SGreg Roach $xml = simplexml_load_file($providersFile); 2021f374598SGreg Roach // need to convert xml structure into arrays & strings 2031f374598SGreg Roach foreach ($xml as $provider) { 2041f374598SGreg Roach $style_keys = array_map( 20518d7a90dSGreg Roach function (string $item): string { 2061f374598SGreg Roach return preg_replace('/[^a-z\d]/i', '', strtolower($item)); 2071f374598SGreg Roach }, 2081f374598SGreg Roach (array) $provider->styles 2091f374598SGreg Roach ); 2101f374598SGreg Roach 2111f374598SGreg Roach $key = preg_replace('/[^a-z\d]/i', '', strtolower((string) $provider->name)); 2121f374598SGreg Roach 2131f374598SGreg Roach self::$map_providers[$key] = [ 2141f374598SGreg Roach 'name' => (string) $provider->name, 2151f374598SGreg Roach 'styles' => array_combine($style_keys, (array) $provider->styles), 2161f374598SGreg Roach ]; 2171f374598SGreg Roach } 2181f374598SGreg Roach } catch (Exception $ex) { 2191f374598SGreg Roach // Default provider is OpenStreetMap 2201f374598SGreg Roach self::$map_selections = [ 2211f374598SGreg Roach 'provider' => 'openstreetmap', 2221f374598SGreg Roach 'style' => 'mapnik', 2231f374598SGreg Roach ]; 2241f374598SGreg Roach self::$map_providers = [ 2251f374598SGreg Roach 'openstreetmap' => [ 2261f374598SGreg Roach 'name' => 'OpenStreetMap', 2271f374598SGreg Roach 'styles' => ['mapnik' => 'Mapnik'], 2281f374598SGreg Roach ], 2291f374598SGreg Roach ]; 2301f374598SGreg Roach }; 2311f374598SGreg Roach } 2321f374598SGreg Roach 2331f374598SGreg Roach //Ugly!!! 2341f374598SGreg Roach switch ($request->get('action')) { 2351f374598SGreg Roach case 'BaseData': 2361f374598SGreg Roach $varName = (self::$map_selections['style'] === '') ? '' : self::$map_providers[self::$map_selections['provider']]['styles'][self::$map_selections['style']]; 2371f374598SGreg Roach $payload = [ 2381f374598SGreg Roach 'selectedProvIndex' => self::$map_selections['provider'], 2391f374598SGreg Roach 'selectedProvName' => self::$map_providers[self::$map_selections['provider']]['name'], 2401f374598SGreg Roach 'selectedStyleName' => $varName, 2411f374598SGreg Roach ]; 2421f374598SGreg Roach break; 2431f374598SGreg Roach case 'ProviderStyles': 2441f374598SGreg Roach $provider = $request->get('provider', 'openstreetmap'); 2451f374598SGreg Roach $payload = self::$map_providers[$provider]['styles']; 2461f374598SGreg Roach break; 2471f374598SGreg Roach case 'AdminConfig': 2481f374598SGreg Roach $providers = []; 2491f374598SGreg Roach foreach (self::$map_providers as $key => $provider) { 2501f374598SGreg Roach $providers[$key] = $provider['name']; 2511f374598SGreg Roach } 2521f374598SGreg Roach $payload = [ 2531f374598SGreg Roach 'providers' => $providers, 2541f374598SGreg Roach 'selectedProv' => self::$map_selections['provider'], 2551f374598SGreg Roach 'styles' => self::$map_providers[self::$map_selections['provider']]['styles'], 2561f374598SGreg Roach 'selectedStyle' => self::$map_selections['style'], 2571f374598SGreg Roach ]; 2581f374598SGreg Roach break; 2591f374598SGreg Roach default: 2601f374598SGreg Roach $payload = null; 2611f374598SGreg Roach } 2621f374598SGreg Roach 2631f374598SGreg Roach return $payload; 2641f374598SGreg Roach } 2651f374598SGreg Roach} 266