11f374598SGreg Roach<?php 23976b470SGreg Roach 31f374598SGreg Roach/** 41f374598SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 151f374598SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 161f374598SGreg Roach */ 17fcfa147eSGreg Roach 181f374598SGreg Roachdeclare(strict_types=1); 191f374598SGreg Roach 201f374598SGreg Roachnamespace Fisharebest\Webtrees\Module; 211f374598SGreg Roach 221f374598SGreg Roachuse Exception; 231f374598SGreg Roachuse Fisharebest\Webtrees\Fact; 248af6bbf8SGreg Roachuse Fisharebest\Webtrees\Family; 258af6bbf8SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 261f374598SGreg Roachuse Fisharebest\Webtrees\I18N; 271f374598SGreg Roachuse Fisharebest\Webtrees\Individual; 288af6bbf8SGreg Roachuse Fisharebest\Webtrees\Location; 29840bd0d7SGreg Roachuse Fisharebest\Webtrees\Site; 3039ca88baSGreg Roachuse Illuminate\Support\Collection; 3105ff554cSGreg Roachuse stdClass; 321f374598SGreg Roach 331f374598SGreg Roach/** 341f374598SGreg Roach * Class PlacesMapModule 351f374598SGreg Roach */ 3637eb8894SGreg Roachclass PlacesModule extends AbstractModule implements ModuleTabInterface 371f374598SGreg Roach{ 3849a243cbSGreg Roach use ModuleTabTrait; 3949a243cbSGreg Roach 401191c60cSGreg Roach protected const ICONS = [ 4180993423SGreg Roach 'BIRT' => ['color' => 'pink', 'name' => 'baby-carriage fas'], 4280993423SGreg Roach 'BAPM' => ['color' => 'pink', 'name' => 'water fas'], 4380993423SGreg Roach 'BARM' => ['color' => 'pink', 'name' => 'star-of-david fas'], 4480993423SGreg Roach 'BASM' => ['color' => 'pink', 'name' => 'star-of-david fas'], 4580993423SGreg Roach 'CHR' => ['color' => 'pink', 'name' => 'water fas'], 4680993423SGreg Roach 'CHRA' => ['color' => 'pink', 'name' => 'water fas'], 4780993423SGreg Roach 'MARR' => ['color' => 'green', 'name' => 'infinity fas'], 4880993423SGreg Roach 'DEAT' => ['color' => 'black', 'name' => 'times fas'], 4980993423SGreg Roach 'BURI' => ['color' => 'purple', 'name' => 'times fas'], 5080993423SGreg Roach 'CREM' => ['color' => 'black', 'name' => 'times fas'], 5180993423SGreg Roach 'CENS' => ['color' => 'cyan', 'name' => 'list fas'], 5280993423SGreg Roach 'RESI' => ['color' => 'cyan', 'name' => 'home fas'], 5380993423SGreg Roach 'OCCU' => ['color' => 'cyan', 'name' => 'industry fas'], 5480993423SGreg Roach 'GRAD' => ['color' => 'violet', 'name' => 'university fas'], 5580993423SGreg Roach 'EDUC' => ['color' => 'violet', 'name' => 'university fas'], 568af6bbf8SGreg Roach ]; 578af6bbf8SGreg Roach 5857862dd5SDavid Drury protected const DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye fas']; 598af6bbf8SGreg Roach 60961ec755SGreg Roach /** 610cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 62961ec755SGreg Roach * 63961ec755SGreg Roach * @return string 64961ec755SGreg Roach */ 6549a243cbSGreg Roach public function title(): string 661f374598SGreg Roach { 67bbb76c12SGreg Roach /* I18N: Name of a module */ 68bbb76c12SGreg Roach return I18N::translate('Places'); 691f374598SGreg Roach } 701f374598SGreg Roach 71961ec755SGreg Roach /** 72961ec755SGreg Roach * A sentence describing what this module does. 73961ec755SGreg Roach * 74961ec755SGreg Roach * @return string 75961ec755SGreg Roach */ 7649a243cbSGreg Roach public function description(): string 771f374598SGreg Roach { 78d21f0b10SGreg Roach /* I18N: Description of the “Places” module */ 79bbb76c12SGreg Roach return I18N::translate('Show the location of events on a map.'); 801f374598SGreg Roach } 811f374598SGreg Roach 8249a243cbSGreg Roach /** 8349a243cbSGreg Roach * The default position for this tab. It can be changed in the control panel. 8449a243cbSGreg Roach * 8549a243cbSGreg Roach * @return int 8649a243cbSGreg Roach */ 87cbf4b7faSGreg Roach public function defaultTabOrder(): int 88cbf4b7faSGreg Roach { 89fb7a0427SGreg Roach return 8; 901f374598SGreg Roach } 911f374598SGreg Roach 923caaa4d2SGreg Roach /** 933caaa4d2SGreg Roach * Is this tab empty? If so, we don't always need to display it. 943caaa4d2SGreg Roach * 953caaa4d2SGreg Roach * @param Individual $individual 963caaa4d2SGreg Roach * 973caaa4d2SGreg Roach * @return bool 983caaa4d2SGreg Roach */ 998f53f488SRico Sonntag public function hasTabContent(Individual $individual): bool 1001f374598SGreg Roach { 101840bd0d7SGreg Roach return Site::getPreference('map-provider') !== ''; 1021f374598SGreg Roach } 1031f374598SGreg Roach 1043caaa4d2SGreg Roach /** 1053caaa4d2SGreg Roach * A greyed out tab has no actual content, but may perhaps have 1063caaa4d2SGreg Roach * options to create content. 1073caaa4d2SGreg Roach * 1083caaa4d2SGreg Roach * @param Individual $individual 1093caaa4d2SGreg Roach * 1103caaa4d2SGreg Roach * @return bool 1113caaa4d2SGreg Roach */ 1128f53f488SRico Sonntag public function isGrayedOut(Individual $individual): bool 1131f374598SGreg Roach { 1141f374598SGreg Roach return false; 1151f374598SGreg Roach } 1161f374598SGreg Roach 1173caaa4d2SGreg Roach /** 1183caaa4d2SGreg Roach * Can this tab load asynchronously? 1193caaa4d2SGreg Roach * 1203caaa4d2SGreg Roach * @return bool 1213caaa4d2SGreg Roach */ 1228f53f488SRico Sonntag public function canLoadAjax(): bool 1231f374598SGreg Roach { 1241f374598SGreg Roach return true; 1251f374598SGreg Roach } 1261f374598SGreg Roach 1273caaa4d2SGreg Roach /** 1283caaa4d2SGreg Roach * Generate the HTML content of this tab. 1293caaa4d2SGreg Roach * 1303caaa4d2SGreg Roach * @param Individual $individual 1313caaa4d2SGreg Roach * 1323caaa4d2SGreg Roach * @return string 1333caaa4d2SGreg Roach */ 1349b34404bSGreg Roach public function getTabContent(Individual $individual): string 1351f374598SGreg Roach { 136aacdcb0dSGreg Roach return view('modules/places/tab', [ 13705ff554cSGreg Roach 'data' => $this->getMapData($individual), 138*597fb44bSDavid Drury 'provider' => [ 139*597fb44bSDavid Drury 'name' => 'OpenStreetMap.Mapnik', 140*597fb44bSDavid Drury 'options' => [] 141*597fb44bSDavid Drury ] 142aacdcb0dSGreg Roach ]); 1431f374598SGreg Roach } 1441f374598SGreg Roach 1451f374598SGreg Roach /** 146e93111adSRico Sonntag * @param Individual $indi 1471f374598SGreg Roach * 14805ff554cSGreg Roach * @return stdClass 1491f374598SGreg Roach */ 15005ff554cSGreg Roach private function getMapData(Individual $indi): stdClass 1511f374598SGreg Roach { 15205ff554cSGreg Roach $facts = $this->getPersonalFacts($indi); 1531f374598SGreg Roach 1541f374598SGreg Roach $geojson = [ 1551f374598SGreg Roach 'type' => 'FeatureCollection', 1561f374598SGreg Roach 'features' => [], 1571f374598SGreg Roach ]; 15805ff554cSGreg Roach 1591f374598SGreg Roach foreach ($facts as $id => $fact) { 1608af6bbf8SGreg Roach $location = new Location($fact->place()->gedcomName()); 1618af6bbf8SGreg Roach 1628af6bbf8SGreg Roach // Use the co-ordinates from the fact (if they exist). 1638af6bbf8SGreg Roach $latitude = $fact->latitude(); 1648af6bbf8SGreg Roach $longitude = $fact->longitude(); 1658af6bbf8SGreg Roach 1668af6bbf8SGreg Roach // Use the co-ordinates from the location otherwise. 1678af6bbf8SGreg Roach if ($latitude === 0.0 && $longitude === 0.0) { 1688af6bbf8SGreg Roach $latitude = $location->latitude(); 1698af6bbf8SGreg Roach $longitude = $location->longitude(); 1708af6bbf8SGreg Roach } 1718af6bbf8SGreg Roach 1728af6bbf8SGreg Roach if ($latitude !== 0.0 || $longitude !== 0.0) { 1731f374598SGreg Roach $geojson['features'][] = [ 1741f374598SGreg Roach 'type' => 'Feature', 1751f374598SGreg Roach 'id' => $id, 1761f374598SGreg Roach 'geometry' => [ 1771f374598SGreg Roach 'type' => 'Point', 178f465e1eaSGreg Roach 'coordinates' => [$longitude, $latitude], 1791f374598SGreg Roach ], 1801f374598SGreg Roach 'properties' => [ 1814b082fd8SGreg Roach 'icon' => static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON, 1828af6bbf8SGreg Roach 'tooltip' => strip_tags($fact->place()->fullName()), 1838af6bbf8SGreg Roach 'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)), 1848af6bbf8SGreg Roach 'zoom' => $location->zoom(), 1851f374598SGreg Roach ], 1861f374598SGreg Roach ]; 1871f374598SGreg Roach } 1881f374598SGreg Roach } 1891f374598SGreg Roach 19005ff554cSGreg Roach return (object) $geojson; 1911f374598SGreg Roach } 1921f374598SGreg Roach 1931f374598SGreg Roach /** 19405ff554cSGreg Roach * @param Individual $individual 195c8a5344dSGreg Roach * 196b5c8fd7eSGreg Roach * @return Collection<Fact> 197c8a5344dSGreg Roach * @throws Exception 198c8a5344dSGreg Roach */ 199c8a5344dSGreg Roach private function getPersonalFacts(Individual $individual): Collection 200c8a5344dSGreg Roach { 201c8a5344dSGreg Roach $facts = $individual->facts(); 202c8a5344dSGreg Roach 203c8a5344dSGreg Roach foreach ($individual->spouseFamilies() as $family) { 204c8a5344dSGreg Roach $facts = $facts->merge($family->facts()); 205c8a5344dSGreg Roach // Add birth of children from this family to the facts array 206c8a5344dSGreg Roach foreach ($family->children() as $child) { 207c8a5344dSGreg Roach $childsBirth = $child->facts(['BIRT'])->first(); 208c8a5344dSGreg Roach if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') { 209c8a5344dSGreg Roach $facts->push($childsBirth); 210c8a5344dSGreg Roach } 211c8a5344dSGreg Roach } 212c8a5344dSGreg Roach } 213c8a5344dSGreg Roach 214c8a5344dSGreg Roach $facts = Fact::sortFacts($facts); 215c8a5344dSGreg Roach 216c8a5344dSGreg Roach return $facts->filter(static function (Fact $item): bool { 217c8a5344dSGreg Roach return $item->place()->gedcomName() !== ''; 218c8a5344dSGreg Roach }); 219c8a5344dSGreg Roach } 220c8a5344dSGreg Roach 221c8a5344dSGreg Roach /** 222c8a5344dSGreg Roach * @param Individual $individual 2238af6bbf8SGreg Roach * @param Fact $fact 2248af6bbf8SGreg Roach * 2258af6bbf8SGreg Roach * @return mixed[] 2268af6bbf8SGreg Roach */ 2278af6bbf8SGreg Roach private function summaryData(Individual $individual, Fact $fact): array 2288af6bbf8SGreg Roach { 2298af6bbf8SGreg Roach $record = $fact->record(); 2308af6bbf8SGreg Roach $name = ''; 2318af6bbf8SGreg Roach $url = ''; 2328af6bbf8SGreg Roach $tag = $fact->label(); 2338af6bbf8SGreg Roach 2348af6bbf8SGreg Roach if ($record instanceof Family) { 2358af6bbf8SGreg Roach // Marriage 23639ca88baSGreg Roach $spouse = $record->spouse($individual); 2378af6bbf8SGreg Roach if ($spouse instanceof Individual) { 2388af6bbf8SGreg Roach $url = $spouse->url(); 23939ca88baSGreg Roach $name = $spouse->fullName(); 2408af6bbf8SGreg Roach } 2418af6bbf8SGreg Roach } elseif ($record !== $individual) { 2428af6bbf8SGreg Roach // Birth of a child 2438af6bbf8SGreg Roach $url = $record->url(); 24439ca88baSGreg Roach $name = $record->fullName(); 2458af6bbf8SGreg Roach $tag = GedcomTag::getLabel('_BIRT_CHIL', $record); 2468af6bbf8SGreg Roach } 2478af6bbf8SGreg Roach 2488af6bbf8SGreg Roach return [ 2498af6bbf8SGreg Roach 'tag' => $tag, 2508af6bbf8SGreg Roach 'url' => $url, 2518af6bbf8SGreg Roach 'name' => $name, 2528af6bbf8SGreg Roach 'value' => $fact->value(), 2538af6bbf8SGreg Roach 'date' => $fact->date()->display(true), 2548af6bbf8SGreg Roach 'place' => $fact->place(), 2558af6bbf8SGreg Roach 'addtag' => false, 2568af6bbf8SGreg Roach ]; 2578af6bbf8SGreg Roach } 2581f374598SGreg Roach} 259