1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Exception; 21use Fisharebest\Webtrees\Auth; 22use Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException; 23use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 24use Fisharebest\Webtrees\Fact; 25use Fisharebest\Webtrees\FactLocation; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Menu; 29use Fisharebest\Webtrees\Tree; 30use Symfony\Component\HttpFoundation\JsonResponse; 31use Symfony\Component\HttpFoundation\Request; 32 33/** 34 * Class PedigreeMapModule 35 */ 36class PedigreeMapModule extends AbstractModule implements ModuleChartInterface 37{ 38 const OSM_MIN_ZOOM = 2; 39 const LINE_COLORS = [ 40 '#FF0000', 41 // Red 42 '#00FF00', 43 // Green 44 '#0000FF', 45 // Blue 46 '#FFB300', 47 // Gold 48 '#00FFFF', 49 // Cyan 50 '#FF00FF', 51 // Purple 52 '#7777FF', 53 // Light blue 54 '#80FF80' 55 // Light green 56 ]; 57 58 private static $map_providers = null; 59 private static $map_selections = null; 60 61 /** {@inheritdoc} */ 62 public function getTitle() 63 { 64 return /* I18N: Name of a module */ 65 I18N::translate('Pedigree map'); 66 } 67 68 /** {@inheritdoc} */ 69 public function getDescription() 70 { 71 return /* I18N: Description of the “OSM” module */ 72 I18N::translate('Show the birthplace of ancestors on a map.'); 73 } 74 75 /** {@inheritdoc} */ 76 public function defaultAccessLevel() 77 { 78 return Auth::PRIV_PRIVATE; 79 } 80 81 /** 82 * Return a menu item for this chart. 83 * 84 * @param Individual $individual 85 * 86 * @return Menu 87 */ 88 public function getChartMenu(Individual $individual) 89 { 90 return new Menu( 91 I18N::translate('Pedigree map'), 92 route('module', [ 93 'module' => $this->getName(), 94 'action' => 'PedigreeMap', 95 'xref' => $individual->getXref(), 96 ]), 97 'menu-chart-pedigreemap', 98 ['rel' => 'nofollow'] 99 ); 100 } 101 102 /** 103 * Return a menu item for this chart - for use in individual boxes. 104 * 105 * @param Individual $individual 106 * 107 * @return Menu 108 */ 109 public function getBoxChartMenu(Individual $individual) 110 { 111 return $this->getChartMenu($individual); 112 } 113 114 /** 115 * @param Request $request 116 * 117 * @return JsonResponse 118 */ 119 public function getBaseDataAction(Request $request): JsonResponse 120 { 121 $provider = $this->getMapProviderData($request); 122 $style = $provider['selectedStyleName'] = '' ? '' : '.' . $provider['selectedStyleName']; 123 124 switch ($provider['selectedProvIndex']) { 125 case 'mapbox': 126 $providerOptions = [ 127 'id' => $this->getPreference('mapbox_id'), 128 'accessToken' => $this->getPreference('mapbox_token'), 129 ]; 130 break; 131 case 'here': 132 $providerOptions = [ 133 'app_id' => $this->getPreference('here_appid'), 134 'app_code' => $this->getPreference('here_appcode'), 135 ]; 136 break; 137 default: 138 $providerOptions = []; 139 }; 140 141 $options = [ 142 'minZoom' => self::OSM_MIN_ZOOM, 143 'providerName' => $provider['selectedProvName'] . $style, 144 'providerOptions' => $providerOptions, 145 'animate' => $this->getPreference('map_animate', 0), 146 'I18N' => [ 147 'zoomInTitle' => I18N::translate('Zoom in'), 148 'zoomOutTitle' => I18N::translate('Zoom out'), 149 'reset' => I18N::translate('Reset to initial map state'), 150 'noData' => I18N::translate('No mappable items'), 151 'error' => I18N::translate('An unknown error occurred'), 152 ], 153 ]; 154 155 return new JsonResponse($options); 156 } 157 158 /** 159 * @param Request $request 160 * 161 * @return JsonResponse 162 * @throws Exception 163 */ 164 public function getMapDataAction(Request $request): JsonResponse 165 { 166 $xref = $request->get('reference'); 167 $tree = $request->attributes->get('tree'); 168 $indi = Individual::getInstance($xref, $tree); 169 $color_count = count(self::LINE_COLORS); 170 171 $facts = $this->getPedigreeMapFacts($request); 172 173 $geojson = [ 174 'type' => 'FeatureCollection', 175 'features' => [], 176 ]; 177 if (empty($facts)) { 178 $code = 204; 179 } else { 180 $code = 200; 181 foreach ($facts as $id => $fact) { 182 $event = new FactLocation($fact, $indi); 183 $icon = $event->getIconDetails(); 184 if ($event->knownLatLon()) { 185 $polyline = null; 186 $color = self::LINE_COLORS[log($id, 2) % $color_count]; 187 $icon['color'] = $color; //make icon color the same as the line 188 $sosa_points[$id] = $event->getLatLonJSArray(); 189 $sosa_parent = (int)floor($id / 2); 190 if (array_key_exists($sosa_parent, $sosa_points)) { 191 // Would like to use a GeometryCollection to hold LineStrings 192 // rather than generate polylines but the MarkerCluster library 193 // doesn't seem to like them 194 $polyline = [ 195 'points' => [ 196 $sosa_points[$sosa_parent], 197 $event->getLatLonJSArray(), 198 ], 199 'options' => [ 200 'color' => $color, 201 ], 202 ]; 203 } 204 $geojson['features'][] = [ 205 'type' => 'Feature', 206 'id' => $id, 207 'valid' => true, 208 'geometry' => [ 209 'type' => 'Point', 210 'coordinates' => $event->getGeoJsonCoords(), 211 ], 212 'properties' => [ 213 'polyline' => $polyline, 214 'icon' => $icon, 215 'tooltip' => $event->toolTip(), 216 'summary' => view('modules/pedigree-map/event-sidebar', $event->shortSummary('pedigree', $id)), 217 'zoom' => (int)$event->getZoom(), 218 ], 219 ]; 220 } 221 } 222 } 223 224 return new JsonResponse($geojson, $code); 225 } 226 227 /** 228 * @param Request $request 229 * 230 * @return array 231 * @throws Exception 232 */ 233 private function getPedigreeMapFacts(Request $request) 234 { 235 $xref = $request->get('reference'); 236 $tree = $request->attributes->get('tree'); 237 $individual = Individual::getInstance($xref, $tree); 238 $generations = (int)$request->get( 239 'generations', 240 $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS') 241 ); 242 $ancestors = $this->sosaStradonitzAncestors($individual, $generations); 243 $facts = []; 244 foreach ($ancestors as $sosa => $person) { 245 if ($person !== null && $person->canShow()) { 246 /** @var Fact $birth */ 247 $birth = $person->getFirstFact('BIRT'); 248 if ($birth && !$birth->getPlace()->isEmpty()) { 249 $facts[$sosa] = $birth; 250 } 251 } 252 } 253 254 return $facts; 255 } 256 257 /** 258 * @param Request $request 259 * 260 * @return JsonResponse 261 */ 262 public function getProviderStylesAction(Request $request): JsonResponse 263 { 264 $styles = $this->getMapProviderData($request); 265 266 return new JsonResponse($styles); 267 } 268 269 /** 270 * @param Request $request 271 * 272 * @return array|null 273 */ 274 private function getMapProviderData(Request $request) 275 { 276 if (self::$map_providers === null) { 277 $providersFile = WT_ROOT . WT_MODULES_DIR . 'openstreetmap/providers/providers.xml'; 278 self::$map_selections = [ 279 'provider' => $this->getPreference('provider', 'openstreetmap'), 280 'style' => $this->getPreference('provider_style', 'mapnik'), 281 ]; 282 283 try { 284 $xml = simplexml_load_file($providersFile); 285 // need to convert xml structure into arrays & strings 286 foreach ($xml as $provider) { 287 $style_keys = array_map( 288 function ($item) { 289 return preg_replace('/[^a-z\d]/i', '', strtolower($item)); 290 }, 291 (array)$provider->styles 292 ); 293 294 $key = preg_replace('/[^a-z\d]/i', '', strtolower((string)$provider->name)); 295 296 self::$map_providers[$key] = [ 297 'name' => (string)$provider->name, 298 'styles' => array_combine($style_keys, (array)$provider->styles), 299 ]; 300 } 301 } catch (Exception $ex) { 302 // Default provider is OpenStreetMap 303 self::$map_selections = [ 304 'provider' => 'openstreetmap', 305 'style' => 'mapnik', 306 ]; 307 self::$map_providers = [ 308 'openstreetmap' => [ 309 'name' => 'OpenStreetMap', 310 'styles' => ['mapnik' => 'Mapnik'], 311 ], 312 ]; 313 }; 314 } 315 316 //Ugly!!! 317 switch ($request->get('action')) { 318 case 'BaseData': 319 $varName = (self::$map_selections['style'] === '') ? '' : self::$map_providers[self::$map_selections['provider']]['styles'][self::$map_selections['style']]; 320 $payload = [ 321 'selectedProvIndex' => self::$map_selections['provider'], 322 'selectedProvName' => self::$map_providers[self::$map_selections['provider']]['name'], 323 'selectedStyleName' => $varName, 324 ]; 325 break; 326 case 'ProviderStyles': 327 $provider = $request->get('provider', 'openstreetmap'); 328 $payload = self::$map_providers[$provider]['styles']; 329 break; 330 case 'AdminConfig': 331 $providers = []; 332 foreach (self::$map_providers as $key => $provider) { 333 $providers[$key] = $provider['name']; 334 } 335 $payload = [ 336 'providers' => $providers, 337 'selectedProv' => self::$map_selections['provider'], 338 'styles' => self::$map_providers[self::$map_selections['provider']]['styles'], 339 'selectedStyle' => self::$map_selections['style'], 340 ]; 341 break; 342 default: 343 $payload = null; 344 } 345 346 return $payload; 347 } 348 349 /** 350 * @param Request $request 351 * 352 * @return object 353 * @throws Exception 354 */ 355 public function getPedigreeMapAction(Request $request) 356 { 357 /** @var Tree $tree */ 358 $tree = $request->attributes->get('tree'); 359 $xref = $request->get('xref'); 360 $individual = Individual::getInstance($xref, $tree); 361 $maxgenerations = $tree->getPreference('MAX_PEDIGREE_GENERATIONS'); 362 $generations = $request->get('generations', $tree->getPreference('DEFAULT_PEDIGREE_GENERATIONS')); 363 364 if ($individual === null) { 365 throw new IndividualNotFoundException; 366 } elseif (!$individual->canShow()) { 367 throw new IndividualAccessDeniedException; 368 } 369 370 return (object)[ 371 'name' => 'modules/pedigree-map/pedigree-map-page', 372 'data' => [ 373 'module' => $this->getName(), 374 'title' => /* I18N: %s is an individual’s name */ 375 I18N::translate('Pedigree map of %s', $individual->getFullName()), 376 'tree' => $tree, 377 'individual' => $individual, 378 'generations' => $generations, 379 'maxgenerations' => $maxgenerations, 380 'map' => view( 381 'modules/pedigree-map/pedigree-map', 382 [ 383 'module' => $this->getName(), 384 'ref' => $individual->getXref(), 385 'type' => 'pedigree', 386 'generations' => $generations, 387 ] 388 ), 389 ], 390 ]; 391 } 392 393 // @TODO shift the following function to somewhere more appropriate during restructure 394 395 /** 396 * Copied from AbstractChartController.php 397 * 398 * Find the ancestors of an individual, and generate an array indexed by 399 * Sosa-Stradonitz number. 400 * 401 * @param Individual $individual Start with this individual 402 * @param int $generations Fetch this number of generations 403 * 404 * @return Individual[] 405 */ 406 private function sosaStradonitzAncestors(Individual $individual, int $generations): array 407 { 408 /** @var Individual[] $ancestors */ 409 $ancestors = [ 410 1 => $individual, 411 ]; 412 413 for ($i = 1, $max = 2 ** ($generations - 1); $i < $max; $i++) { 414 $ancestors[$i * 2] = null; 415 $ancestors[$i * 2 + 1] = null; 416 417 $individual = $ancestors[$i]; 418 419 if ($individual !== null) { 420 $family = $individual->getPrimaryChildFamily(); 421 if ($family !== null) { 422 if ($family->getHusband() !== null) { 423 $ancestors[$i * 2] = $family->getHusband(); 424 } 425 if ($family->getWife() !== null) { 426 $ancestors[$i * 2 + 1] = $family->getWife(); 427 } 428 } 429 } 430 } 431 432 return $ancestors; 433 } 434} 435