1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 <https://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\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Place; 28use Fisharebest\Webtrees\PlaceLocation; 29use Fisharebest\Webtrees\Services\LeafletJsService; 30use Fisharebest\Webtrees\Services\ModuleService; 31use Illuminate\Support\Collection; 32 33/** 34 * Class PlacesMapModule 35 */ 36class PlacesModule extends AbstractModule implements ModuleTabInterface 37{ 38 use ModuleTabTrait; 39 40 protected const array ICONS = [ 41 'FAM:CENS' => ['color' => 'darkcyan', 'name' => 'list fas'], 42 'FAM:MARR' => ['color' => 'green', 'name' => 'infinity fas'], 43 'INDI:BAPM' => ['color' => 'hotpink', 'name' => 'water fas'], 44 'INDI:BARM' => ['color' => 'hotpink', 'name' => 'star-of-david fas'], 45 'INDI:BASM' => ['color' => 'hotpink', 'name' => 'star-of-david fas'], 46 'INDI:BIRT' => ['color' => 'hotpink', 'name' => 'baby-carriage fas'], 47 'INDI:BURI' => ['color' => 'purple', 'name' => 'times fas'], 48 'INDI:CENS' => ['color' => 'darkcyan', 'name' => 'list fas'], 49 'INDI:CHR' => ['color' => 'hotpink', 'name' => 'water fas'], 50 'INDI:CHRA' => ['color' => 'hotpink', 'name' => 'water fas'], 51 'INDI:CREM' => ['color' => 'black', 'name' => 'times fas'], 52 'INDI:DEAT' => ['color' => 'black', 'name' => 'times fas'], 53 'INDI:EDUC' => ['color' => 'violet', 'name' => 'university fas'], 54 'INDI:GRAD' => ['color' => 'violet', 'name' => 'university fas'], 55 'INDI:OCCU' => ['color' => 'darkcyan', 'name' => 'industry fas'], 56 'INDI:RESI' => ['color' => 'darkcyan', 'name' => 'home fas'], 57 ]; 58 59 protected const array OWN_ICONS = [ 60 'INDI:BIRT' => ['color' => 'red', 'name' => 'baby-carriage fas'], 61 'INDI:CHR' => ['color' => 'red', 'name' => 'water fas'], 62 ] + self::ICONS; 63 64 protected const array DEFAULT_ICON = ['color' => 'gold', 'name' => 'bullseye fas']; 65 66 private LeafletJsService $leaflet_js_service; 67 68 private ModuleService $module_service; 69 70 /** 71 * @param LeafletJsService $leaflet_js_service 72 * @param ModuleService $module_service 73 */ 74 public function __construct(LeafletJsService $leaflet_js_service, ModuleService $module_service) 75 { 76 $this->leaflet_js_service = $leaflet_js_service; 77 $this->module_service = $module_service; 78 } 79 80 /** 81 * How should this module be identified in the control panel, etc.? 82 * 83 * @return string 84 */ 85 public function title(): string 86 { 87 /* I18N: Name of a module */ 88 return I18N::translate('Places'); 89 } 90 91 public function description(): string 92 { 93 /* I18N: Description of the “Places” module */ 94 return I18N::translate('Show the location of events on a map.'); 95 } 96 97 /** 98 * The default position for this tab. It can be changed in the control panel. 99 * 100 * @return int 101 */ 102 public function defaultTabOrder(): int 103 { 104 return 8; 105 } 106 107 /** 108 * Is this tab empty? If so, we don't always need to display it. 109 * 110 * @param Individual $individual 111 * 112 * @return bool 113 */ 114 public function hasTabContent(Individual $individual): bool 115 { 116 $map_providers = $this->module_service->findByInterface(ModuleMapProviderInterface::class); 117 118 return $map_providers->isNotEmpty() && $this->getMapData($individual)->features !== []; 119 } 120 121 /** 122 * @param Individual $indi 123 * 124 * @return object 125 */ 126 private function getMapData(Individual $indi): object 127 { 128 $facts = $this->getPersonalFacts($indi); 129 130 $geojson = [ 131 'type' => 'FeatureCollection', 132 'features' => [], 133 ]; 134 135 foreach ($facts as $id => $fact) { 136 $location = new PlaceLocation($fact->place()->gedcomName()); 137 138 // Use the co-ordinates from the fact (if they exist). 139 $latitude = $fact->latitude(); 140 $longitude = $fact->longitude(); 141 142 // Use the co-ordinates from the location otherwise. 143 if ($latitude === null || $longitude === null) { 144 $latitude = $location->latitude(); 145 $longitude = $location->longitude(); 146 } 147 148 $icons = $fact->record() === $indi ? static::OWN_ICONS : static::ICONS; 149 150 if ($latitude !== null && $longitude !== null) { 151 $geojson['features'][] = [ 152 'type' => 'Feature', 153 'id' => $id, 154 'geometry' => [ 155 'type' => 'Point', 156 'coordinates' => [$longitude, $latitude], 157 ], 158 'properties' => [ 159 'icon' => $icons[$fact->tag()] ?? static::DEFAULT_ICON, 160 'tooltip' => $fact->place()->gedcomName(), 161 'summary' => view('modules/places/event-sidebar', $this->summaryData($indi, $fact)), 162 ], 163 ]; 164 } 165 } 166 167 return (object) $geojson; 168 } 169 170 /** 171 * @param Individual $individual 172 * 173 * @return Collection<int,Fact> 174 * @throws Exception 175 */ 176 private function getPersonalFacts(Individual $individual): Collection 177 { 178 $facts = $individual->facts(); 179 180 foreach ($individual->spouseFamilies() as $family) { 181 $facts = $facts->merge($family->facts()); 182 // Add birth of children from this family to the facts array 183 foreach ($family->children() as $child) { 184 $childsBirth = $child->facts(['BIRT'])->first(); 185 if ($childsBirth instanceof Fact && $childsBirth->place()->gedcomName() !== '') { 186 $facts->push($childsBirth); 187 } 188 } 189 } 190 191 $facts = Fact::sortFacts($facts); 192 193 return $facts->filter(static function (Fact $item): bool { 194 return $item->place()->gedcomName() !== ''; 195 }); 196 } 197 198 /** 199 * @param Individual $individual 200 * @param Fact $fact 201 * 202 * @return array<string|Place> 203 */ 204 private function summaryData(Individual $individual, Fact $fact): array 205 { 206 $record = $fact->record(); 207 $name = ''; 208 $url = ''; 209 $tag = $fact->label(); 210 211 if ($record instanceof Family) { 212 // Marriage 213 $spouse = $record->spouse($individual); 214 if ($spouse instanceof Individual) { 215 $url = $spouse->url(); 216 $name = $spouse->fullName(); 217 } 218 } elseif ($record !== $individual) { 219 // Birth of a child 220 $url = $record->url(); 221 $name = $record->fullName(); 222 $tag = I18N::translate('Birth of a child'); 223 } 224 225 return [ 226 'tag' => $tag, 227 'url' => $url, 228 'name' => $name, 229 'value' => $fact->value(), 230 'date' => $fact->date()->display($individual->tree(), null, true), 231 'place' => $fact->place(), 232 ]; 233 } 234 235 /** 236 * A greyed out tab has no actual content, but may perhaps have 237 * options to create content. 238 * 239 * @param Individual $individual 240 * 241 * @return bool 242 */ 243 public function isGrayedOut(Individual $individual): bool 244 { 245 return false; 246 } 247 248 /** 249 * Can this tab load asynchronously? 250 * 251 * @return bool 252 */ 253 public function canLoadAjax(): bool 254 { 255 return true; 256 } 257 258 /** 259 * Generate the HTML content of this tab. 260 * 261 * @param Individual $individual 262 * 263 * @return string 264 */ 265 public function getTabContent(Individual $individual): string 266 { 267 return view('modules/places/tab', [ 268 'data' => $this->getMapData($individual), 269 'leaflet_config' => $this->leaflet_js_service->config(), 270 ]); 271 } 272} 273