1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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; 21 22use Fisharebest\Webtrees\Services\GedcomService; 23use Illuminate\Database\Capsule\Manager as DB; 24use Illuminate\Support\Collection; 25use stdClass; 26 27use function app; 28use function preg_split; 29 30/** 31 * Class PlaceLocation 32 */ 33class PlaceLocation 34{ 35 /** @var string e.g. "Westminster, London, England" */ 36 private $location_name; 37 38 /** @var Collection<string> The parts of a location name, e.g. ["Westminster", "London", "England"] */ 39 private $parts; 40 41 /** 42 * Create a place-location. 43 * 44 * @param string $location_name 45 */ 46 public function __construct(string $location_name) 47 { 48 // Ignore any empty parts in location names such as "Village, , , Country". 49 $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name))) 50 ->filter(); 51 52 // Rebuild the location name in the correct format. 53 $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 54 } 55 56 /** 57 * Get the higher level location. 58 * 59 * @return PlaceLocation 60 */ 61 public function parent(): PlaceLocation 62 { 63 return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR)); 64 } 65 66 /** 67 * The database row that contains this location. 68 * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 69 * 70 * @return int 71 */ 72 public function id(): int 73 { 74 return app('cache.array')->remember('location-' . $this->location_name, function () { 75 // The "top-level" location won't exist in the database. 76 if ($this->parts->isEmpty()) { 77 return 0; 78 } 79 80 $parent_location_id = $this->parent()->id(); 81 82 $location_id = (int) DB::table('placelocation') 83 ->where('pl_place', '=', mb_substr($this->parts->first(), 0, 120)) 84 ->where('pl_parent_id', '=', $parent_location_id) 85 ->value('pl_id'); 86 87 if ($location_id === 0) { 88 $location = $this->parts->first(); 89 90 $location_id = 1 + (int) DB::table('placelocation')->max('pl_id'); 91 92 DB::table('placelocation')->insert([ 93 'pl_id' => $location_id, 94 'pl_place' => mb_substr($location, 0, 120), 95 'pl_parent_id' => $parent_location_id, 96 'pl_level' => $this->parts->count() - 1, 97 'pl_lati' => '', 98 'pl_long' => '', 99 'pl_icon' => '', 100 'pl_zoom' => 2, 101 ]); 102 } 103 104 return $location_id; 105 }); 106 } 107 108 /** 109 * Does this location exist in the database? Note that calls to PlaceLocation::id() will 110 * create the row, so this function is only meaningful when called before a call to PlaceLocation::id(). 111 * 112 * @return bool 113 */ 114 public function exists(): bool 115 { 116 $location_id = 0; 117 118 $this->parts->reverse()->each(static function (string $place) use (&$location_id) { 119 if ($location_id !== null) { 120 $location_id = DB::table('placelocation') 121 ->where('pl_parent_id', '=', $location_id) 122 ->where('pl_place', '=', mb_substr($place, 0, 120)) 123 ->value('pl_id'); 124 } 125 }); 126 127 return $location_id !== null; 128 } 129 130 /** 131 * @return stdClass 132 */ 133 private function details(): stdClass 134 { 135 return app('cache.array')->remember('location-details-' . $this->id(), function () { 136 // The "top-level" location won't exist in the database. 137 if ($this->parts->isEmpty()) { 138 return (object) [ 139 'pl_id' => '0', 140 'pl_parent_id' => '0', 141 'pl_level' => null, 142 'pl_place' => '', 143 'pl_lati' => null, 144 'pl_long' => null, 145 'pl_zoom' => null, 146 'pl_icon' => null, 147 'pl_media' => null, 148 ]; 149 } 150 151 return DB::table('placelocation') 152 ->where('pl_id', '=', $this->id()) 153 ->first(); 154 }); 155 } 156 157 /** 158 * Latitude of the location. 159 * 160 * @return float 161 */ 162 public function latitude(): float 163 { 164 $gedcom_service = new GedcomService(); 165 166 $tmp = $this; 167 do { 168 $pl_lati = (string) $tmp->details()->pl_lati; 169 $tmp = $tmp->parent(); 170 } while ($pl_lati === '' && $tmp->id() !== 0); 171 172 return $gedcom_service->readLatitude($pl_lati); 173 } 174 175 /** 176 * Latitude of the longitude. 177 * 178 * @return float 179 */ 180 public function longitude(): float 181 { 182 $gedcom_service = new GedcomService(); 183 184 $tmp = $this; 185 do { 186 $pl_long = (string) $tmp->details()->pl_long; 187 $tmp = $tmp->parent(); 188 } while ($pl_long === '' && $tmp->id() !== 0); 189 190 return $gedcom_service->readLongitude($pl_long); 191 } 192 193 /** 194 * The icon for the location. 195 * 196 * @return string 197 */ 198 public function icon(): string 199 { 200 return (string) $this->details()->pl_icon; 201 } 202 203 /** 204 * Zoom level for the location. 205 * 206 * @return int 207 */ 208 public function zoom(): int 209 { 210 return (int) $this->details()->pl_zoom ?: 2; 211 } 212 213 /** 214 * @return string 215 */ 216 public function locationName(): string 217 { 218 return (string) $this->parts->first(); 219 } 220 221 /** 222 * Find a rectangle that (approximately) encloses this place. 223 * 224 * @return array<array<float>> 225 */ 226 public function boundingRectangle(): array 227 { 228 if ($this->id() === 0) { 229 return [[-180.0, -90.0], [180.0, 90.0]]; 230 } 231 232 // Find our own co-ordinates and those of any child places 233 $latitudes = DB::table('placelocation') 234 ->where('pl_parent_id', '=', $this->id()) 235 ->orWhere('pl_id', '=', $this->id()) 236 ->pluck('pl_lati') 237 ->filter() 238 ->map(static function (string $x): float { 239 return (new GedcomService())->readLatitude($x); 240 }); 241 242 $longitudes = DB::table('placelocation') 243 ->where('pl_parent_id', '=', $this->id()) 244 ->orWhere('pl_id', '=', $this->id()) 245 ->pluck('pl_long') 246 ->filter() 247 ->map(static function (string $x): float { 248 return (new GedcomService())->readLongitude($x); 249 }); 250 251 if ($latitudes->count() > 1 || $longitudes->count() > 1) { 252 return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]]; 253 } 254 255 return $this->parent()->boundingRectangle(); 256 } 257} 258