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 $icon = static::ICONS[$fact->getTag()] ?? static::DEFAULT_ICON; 169 170 if ($latitude !== 0.0 || $longitude !== 0.0) { 171 $geojson['features'][] = [ 172 'type' => 'Feature', 173 'id' => $id, 174 'valid' => true, 175 'geometry' => [ 176 'type' => 'Point', 177 'coordinates' => [$longitude, $latitude], 178 ], 179 'properties' => [ 180 'polyline' => null, 181 'icon' => $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 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