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' => 'lightcoral', 'name' => 'baby-carriage'], 42 'BAPM' => ['color' => 'lightcoral', 'name' => 'water'], 43 'BARM' => ['color' => 'lightcoral', 'name' => 'star-of-david'], 44 'BASM' => ['color' => 'lightcoral', 'name' => 'star-of-david'], 45 'CHR' => ['color' => 'lightcoral', 'name' => 'water'], 46 'CHRA' => ['color' => 'lightcoral', 'name' => 'water'], 47 'MARR' => ['color' => 'green', 'name' => 'infinity'], 48 'DEAT' => ['color' => 'black', 'name' => 'times'], 49 'BURI' => ['color' => 'sienna', 'name' => 'times'], 50 'CREM' => ['color' => 'black', 'name' => 'times'], 51 'CENS' => ['color' => 'mediumblue', 'name' => 'list'], 52 'RESI' => ['color' => 'mediumblue', 'name' => 'home'], 53 'OCCU' => ['color' => 'mediumblue', 'name' => 'industry'], 54 'GRAD' => ['color' => 'plum', 'name' => 'university'], 55 'EDUC' => ['color' => 'plum', 'name' => 'university'], 56 ]; 57 58 protected const DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye ']; 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 ]); 139 } 140 141 /** 142 * @param Individual $indi 143 * 144 * @return stdClass 145 */ 146 private function getMapData(Individual $indi): stdClass 147 { 148 $facts = $this->getPersonalFacts($indi); 149 150 $geojson = [ 151 'type' => 'FeatureCollection', 152 'features' => [], 153 ]; 154 155 foreach ($facts as $id => $fact) { 156 $location = new Location($fact->place()->gedcomName()); 157 158 // Use the co-ordinates from the fact (if they exist). 159 $latitude = $fact->latitude(); 160 $longitude = $fact->longitude(); 161 162 // Use the co-ordinates from the location otherwise. 163 if ($latitude === 0.0 && $longitude === 0.0) { 164 $latitude = $location->latitude(); 165 $longitude = $location->longitude(); 166 } 167 168 if ($latitude !== 0.0 || $longitude !== 0.0) { 169 $geojson['features'][] = [ 170 'type' => 'Feature', 171 'id' => $id, 172 'geometry' => [ 173 'type' => 'Point', 174 'coordinates' => [$longitude, $latitude], 175 ], 176 'properties' => [ 177 'polyline' => null, 178 'icon' => static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON, 179 'tooltip' => strip_tags($fact->place()->fullName()), 180 'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)), 181 'zoom' => $location->zoom(), 182 ], 183 ]; 184 } 185 } 186 187 return (object) $geojson; 188 } 189 190 /** 191 * @param Individual $individual 192 * 193 * @return Collection 194 * @throws Exception 195 */ 196 private function getPersonalFacts(Individual $individual): Collection 197 { 198 $facts = $individual->facts(); 199 200 foreach ($individual->spouseFamilies() as $family) { 201 $facts = $facts->merge($family->facts()); 202 // Add birth of children from this family to the facts array 203 foreach ($family->children() as $child) { 204 $childsBirth = $child->facts(['BIRT'])->first(); 205 if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') { 206 $facts->push($childsBirth); 207 } 208 } 209 } 210 211 $facts = Fact::sortFacts($facts); 212 213 return $facts->filter(static function (Fact $item): bool { 214 return $item->place()->gedcomName() !== ''; 215 }); 216 } 217 218 /** 219 * @param Individual $individual 220 * @param Fact $fact 221 * 222 * @return mixed[] 223 */ 224 private function summaryData(Individual $individual, Fact $fact): array 225 { 226 $record = $fact->record(); 227 $name = ''; 228 $url = ''; 229 $tag = $fact->label(); 230 231 if ($record instanceof Family) { 232 // Marriage 233 $spouse = $record->spouse($individual); 234 if ($spouse instanceof Individual) { 235 $url = $spouse->url(); 236 $name = $spouse->fullName(); 237 } 238 } elseif ($record !== $individual) { 239 // Birth of a child 240 $url = $record->url(); 241 $name = $record->fullName(); 242 $tag = GedcomTag::getLabel('_BIRT_CHIL', $record); 243 } 244 245 return [ 246 'tag' => $tag, 247 'url' => $url, 248 'name' => $name, 249 'value' => $fact->value(), 250 'date' => $fact->date()->display(true), 251 'place' => $fact->place(), 252 'addtag' => false, 253 ]; 254 } 255} 256