1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Exception; 23use Fisharebest\Webtrees\Fact; 24use Fisharebest\Webtrees\Family; 25use Fisharebest\Webtrees\GedcomTag; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Location; 29use Fisharebest\Webtrees\Site; 30use Illuminate\Support\Collection; 31use stdClass; 32 33/** 34 * Class PlacesMapModule 35 */ 36class PlacesModule extends AbstractModule implements ModuleTabInterface 37{ 38 use ModuleTabTrait; 39 40 protected const ICONS = [ 41 'BIRT' => ['color' => 'pink', 'name' => 'baby-carriage fas'], 42 'BAPM' => ['color' => 'pink', 'name' => 'water fas'], 43 'BARM' => ['color' => 'pink', 'name' => 'star-of-david fas'], 44 'BASM' => ['color' => 'pink', 'name' => 'star-of-david fas'], 45 'CHR' => ['color' => 'pink', 'name' => 'water fas'], 46 'CHRA' => ['color' => 'pink', 'name' => 'water fas'], 47 'MARR' => ['color' => 'green', 'name' => 'infinity fas'], 48 'DEAT' => ['color' => 'black', 'name' => 'times fas'], 49 'BURI' => ['color' => 'purple', 'name' => 'times fas'], 50 'CREM' => ['color' => 'black', 'name' => 'times fas'], 51 'CENS' => ['color' => 'cyan', 'name' => 'list fas'], 52 'RESI' => ['color' => 'cyan', 'name' => 'home fas'], 53 'OCCU' => ['color' => 'cyan', 'name' => 'industry fas'], 54 'GRAD' => ['color' => 'violet', 'name' => 'university fas'], 55 'EDUC' => ['color' => 'violet', 'name' => 'university fas'], 56 ]; 57 58 protected const DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye fas']; 59 60 /** 61 * How should this module be identified in the control panel, etc.? 62 * 63 * @return string 64 */ 65 public function title(): string 66 { 67 /* I18N: Name of a module */ 68 return I18N::translate('Places'); 69 } 70 71 /** 72 * A sentence describing what this module does. 73 * 74 * @return string 75 */ 76 public function description(): string 77 { 78 /* I18N: Description of the “Places” module */ 79 return I18N::translate('Show the location of events on a map.'); 80 } 81 82 /** 83 * The default position for this tab. It can be changed in the control panel. 84 * 85 * @return int 86 */ 87 public function defaultTabOrder(): int 88 { 89 return 8; 90 } 91 92 /** 93 * Is this tab empty? If so, we don't always need to display it. 94 * 95 * @param Individual $individual 96 * 97 * @return bool 98 */ 99 public function hasTabContent(Individual $individual): bool 100 { 101 return Site::getPreference('map-provider') !== ''; 102 } 103 104 /** 105 * A greyed out tab has no actual content, but may perhaps have 106 * options to create content. 107 * 108 * @param Individual $individual 109 * 110 * @return bool 111 */ 112 public function isGrayedOut(Individual $individual): bool 113 { 114 return false; 115 } 116 117 /** 118 * Can this tab load asynchronously? 119 * 120 * @return bool 121 */ 122 public function canLoadAjax(): bool 123 { 124 return true; 125 } 126 127 /** 128 * Generate the HTML content of this tab. 129 * 130 * @param Individual $individual 131 * 132 * @return string 133 */ 134 public function getTabContent(Individual $individual): string 135 { 136 return view('modules/places/tab', [ 137 'data' => $this->getMapData($individual), 138 'provider' => [ 139 'name' => 'OpenStreetMap.Mapnik', 140 'options' => [] 141 ] 142 ]); 143 } 144 145 /** 146 * @param Individual $indi 147 * 148 * @return stdClass 149 */ 150 private function getMapData(Individual $indi): stdClass 151 { 152 $facts = $this->getPersonalFacts($indi); 153 154 $geojson = [ 155 'type' => 'FeatureCollection', 156 'features' => [], 157 ]; 158 159 foreach ($facts as $id => $fact) { 160 $location = new Location($fact->place()->gedcomName()); 161 162 // Use the co-ordinates from the fact (if they exist). 163 $latitude = $fact->latitude(); 164 $longitude = $fact->longitude(); 165 166 // Use the co-ordinates from the location otherwise. 167 if ($latitude === 0.0 && $longitude === 0.0) { 168 $latitude = $location->latitude(); 169 $longitude = $location->longitude(); 170 } 171 172 if ($latitude !== 0.0 || $longitude !== 0.0) { 173 $geojson['features'][] = [ 174 'type' => 'Feature', 175 'id' => $id, 176 'geometry' => [ 177 'type' => 'Point', 178 'coordinates' => [$longitude, $latitude], 179 ], 180 'properties' => [ 181 'icon' => static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON, 182 'tooltip' => strip_tags($fact->place()->fullName()), 183 'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)), 184 'zoom' => $location->zoom(), 185 ], 186 ]; 187 } 188 } 189 190 return (object) $geojson; 191 } 192 193 /** 194 * @param Individual $individual 195 * 196 * @return Collection<Fact> 197 * @throws Exception 198 */ 199 private function getPersonalFacts(Individual $individual): Collection 200 { 201 $facts = $individual->facts(); 202 203 foreach ($individual->spouseFamilies() as $family) { 204 $facts = $facts->merge($family->facts()); 205 // Add birth of children from this family to the facts array 206 foreach ($family->children() as $child) { 207 $childsBirth = $child->facts(['BIRT'])->first(); 208 if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') { 209 $facts->push($childsBirth); 210 } 211 } 212 } 213 214 $facts = Fact::sortFacts($facts); 215 216 return $facts->filter(static function (Fact $item): bool { 217 return $item->place()->gedcomName() !== ''; 218 }); 219 } 220 221 /** 222 * @param Individual $individual 223 * @param Fact $fact 224 * 225 * @return mixed[] 226 */ 227 private function summaryData(Individual $individual, Fact $fact): array 228 { 229 $record = $fact->record(); 230 $name = ''; 231 $url = ''; 232 $tag = $fact->label(); 233 234 if ($record instanceof Family) { 235 // Marriage 236 $spouse = $record->spouse($individual); 237 if ($spouse instanceof Individual) { 238 $url = $spouse->url(); 239 $name = $spouse->fullName(); 240 } 241 } elseif ($record !== $individual) { 242 // Birth of a child 243 $url = $record->url(); 244 $name = $record->fullName(); 245 $tag = GedcomTag::getLabel('_BIRT_CHIL', $record); 246 } 247 248 return [ 249 'tag' => $tag, 250 'url' => $url, 251 'name' => $name, 252 'value' => $fact->value(), 253 'date' => $fact->date()->display(true), 254 'place' => $fact->place(), 255 'addtag' => false, 256 ]; 257 } 258} 259