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 = [ 41*80993423SGreg Roach 'BIRT' => ['color' => 'pink', 'name' => 'baby-carriage fas'], 42*80993423SGreg Roach 'BAPM' => ['color' => 'pink', 'name' => 'water fas'], 43*80993423SGreg Roach 'BARM' => ['color' => 'pink', 'name' => 'star-of-david fas'], 44*80993423SGreg Roach 'BASM' => ['color' => 'pink', 'name' => 'star-of-david fas'], 45*80993423SGreg Roach 'CHR' => ['color' => 'pink', 'name' => 'water fas'], 46*80993423SGreg Roach 'CHRA' => ['color' => 'pink', 'name' => 'water fas'], 47*80993423SGreg Roach 'MARR' => ['color' => 'green', 'name' => 'infinity fas'], 48*80993423SGreg Roach 'DEAT' => ['color' => 'black', 'name' => 'times fas'], 49*80993423SGreg Roach 'BURI' => ['color' => 'purple', 'name' => 'times fas'], 50*80993423SGreg Roach 'CREM' => ['color' => 'black', 'name' => 'times fas'], 51*80993423SGreg Roach 'CENS' => ['color' => 'cyan', 'name' => 'list fas'], 52*80993423SGreg Roach 'RESI' => ['color' => 'cyan', 'name' => 'home fas'], 53*80993423SGreg Roach 'OCCU' => ['color' => 'cyan', 'name' => 'industry fas'], 54*80993423SGreg Roach 'GRAD' => ['color' => 'violet', 'name' => 'university fas'], 55*80993423SGreg Roach 'EDUC' => ['color' => 'violet', 'name' => 'university fas'], 568af6bbf8SGreg Roach ]; 578af6bbf8SGreg Roach 58*80993423SGreg Roach protected const DEFAULT_ICON = ['color' => 'gold', 'name' => 'fa-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), 138aacdcb0dSGreg Roach ]); 1391f374598SGreg Roach } 1401f374598SGreg Roach 1411f374598SGreg Roach /** 142e93111adSRico Sonntag * @param Individual $indi 1431f374598SGreg Roach * 14405ff554cSGreg Roach * @return stdClass 1451f374598SGreg Roach */ 14605ff554cSGreg Roach private function getMapData(Individual $indi): stdClass 1471f374598SGreg Roach { 14805ff554cSGreg Roach $facts = $this->getPersonalFacts($indi); 1491f374598SGreg Roach 1501f374598SGreg Roach $geojson = [ 1511f374598SGreg Roach 'type' => 'FeatureCollection', 1521f374598SGreg Roach 'features' => [], 1531f374598SGreg Roach ]; 15405ff554cSGreg Roach 1551f374598SGreg Roach foreach ($facts as $id => $fact) { 1568af6bbf8SGreg Roach $location = new Location($fact->place()->gedcomName()); 1578af6bbf8SGreg Roach 1588af6bbf8SGreg Roach // Use the co-ordinates from the fact (if they exist). 1598af6bbf8SGreg Roach $latitude = $fact->latitude(); 1608af6bbf8SGreg Roach $longitude = $fact->longitude(); 1618af6bbf8SGreg Roach 1628af6bbf8SGreg Roach // Use the co-ordinates from the location otherwise. 1638af6bbf8SGreg Roach if ($latitude === 0.0 && $longitude === 0.0) { 1648af6bbf8SGreg Roach $latitude = $location->latitude(); 1658af6bbf8SGreg Roach $longitude = $location->longitude(); 1668af6bbf8SGreg Roach } 1678af6bbf8SGreg Roach 1688af6bbf8SGreg Roach if ($latitude !== 0.0 || $longitude !== 0.0) { 1691f374598SGreg Roach $geojson['features'][] = [ 1701f374598SGreg Roach 'type' => 'Feature', 1711f374598SGreg Roach 'id' => $id, 1721f374598SGreg Roach 'geometry' => [ 1731f374598SGreg Roach 'type' => 'Point', 174f465e1eaSGreg Roach 'coordinates' => [$longitude, $latitude], 1751f374598SGreg Roach ], 1761f374598SGreg Roach 'properties' => [ 17705ff554cSGreg Roach 'polyline' => null, 1784b082fd8SGreg Roach 'icon' => static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON, 1798af6bbf8SGreg Roach 'tooltip' => strip_tags($fact->place()->fullName()), 1808af6bbf8SGreg Roach 'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)), 1818af6bbf8SGreg Roach 'zoom' => $location->zoom(), 1821f374598SGreg Roach ], 1831f374598SGreg Roach ]; 1841f374598SGreg Roach } 1851f374598SGreg Roach } 1861f374598SGreg Roach 18705ff554cSGreg Roach return (object) $geojson; 1881f374598SGreg Roach } 1891f374598SGreg Roach 1901f374598SGreg Roach /** 19105ff554cSGreg Roach * @param Individual $individual 192c8a5344dSGreg Roach * 193c8a5344dSGreg Roach * @return Collection 194c8a5344dSGreg Roach * @throws Exception 195c8a5344dSGreg Roach */ 196c8a5344dSGreg Roach private function getPersonalFacts(Individual $individual): Collection 197c8a5344dSGreg Roach { 198c8a5344dSGreg Roach $facts = $individual->facts(); 199c8a5344dSGreg Roach 200c8a5344dSGreg Roach foreach ($individual->spouseFamilies() as $family) { 201c8a5344dSGreg Roach $facts = $facts->merge($family->facts()); 202c8a5344dSGreg Roach // Add birth of children from this family to the facts array 203c8a5344dSGreg Roach foreach ($family->children() as $child) { 204c8a5344dSGreg Roach $childsBirth = $child->facts(['BIRT'])->first(); 205c8a5344dSGreg Roach if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') { 206c8a5344dSGreg Roach $facts->push($childsBirth); 207c8a5344dSGreg Roach } 208c8a5344dSGreg Roach } 209c8a5344dSGreg Roach } 210c8a5344dSGreg Roach 211c8a5344dSGreg Roach $facts = Fact::sortFacts($facts); 212c8a5344dSGreg Roach 213c8a5344dSGreg Roach return $facts->filter(static function (Fact $item): bool { 214c8a5344dSGreg Roach return $item->place()->gedcomName() !== ''; 215c8a5344dSGreg Roach }); 216c8a5344dSGreg Roach } 217c8a5344dSGreg Roach 218c8a5344dSGreg Roach /** 219c8a5344dSGreg Roach * @param Individual $individual 2208af6bbf8SGreg Roach * @param Fact $fact 2218af6bbf8SGreg Roach * 2228af6bbf8SGreg Roach * @return mixed[] 2238af6bbf8SGreg Roach */ 2248af6bbf8SGreg Roach private function summaryData(Individual $individual, Fact $fact): array 2258af6bbf8SGreg Roach { 2268af6bbf8SGreg Roach $record = $fact->record(); 2278af6bbf8SGreg Roach $name = ''; 2288af6bbf8SGreg Roach $url = ''; 2298af6bbf8SGreg Roach $tag = $fact->label(); 2308af6bbf8SGreg Roach 2318af6bbf8SGreg Roach if ($record instanceof Family) { 2328af6bbf8SGreg Roach // Marriage 23339ca88baSGreg Roach $spouse = $record->spouse($individual); 2348af6bbf8SGreg Roach if ($spouse instanceof Individual) { 2358af6bbf8SGreg Roach $url = $spouse->url(); 23639ca88baSGreg Roach $name = $spouse->fullName(); 2378af6bbf8SGreg Roach } 2388af6bbf8SGreg Roach } elseif ($record !== $individual) { 2398af6bbf8SGreg Roach // Birth of a child 2408af6bbf8SGreg Roach $url = $record->url(); 24139ca88baSGreg Roach $name = $record->fullName(); 2428af6bbf8SGreg Roach $tag = GedcomTag::getLabel('_BIRT_CHIL', $record); 2438af6bbf8SGreg Roach } 2448af6bbf8SGreg Roach 2458af6bbf8SGreg Roach return [ 2468af6bbf8SGreg Roach 'tag' => $tag, 2478af6bbf8SGreg Roach 'url' => $url, 2488af6bbf8SGreg Roach 'name' => $name, 2498af6bbf8SGreg Roach 'value' => $fact->value(), 2508af6bbf8SGreg Roach 'date' => $fact->date()->display(true), 2518af6bbf8SGreg Roach 'place' => $fact->place(), 2528af6bbf8SGreg Roach 'addtag' => false, 2538af6bbf8SGreg Roach ]; 2548af6bbf8SGreg Roach } 2551f374598SGreg Roach} 256