15333da53SGreg Roach<?php 25333da53SGreg Roach 35333da53SGreg Roach/** 45333da53SGreg Roach * webtrees: online genealogy 55333da53SGreg Roach * Copyright (C) 2020 webtrees development team 65333da53SGreg Roach * This program is free software: you can redistribute it and/or modify 75333da53SGreg Roach * it under the terms of the GNU General Public License as published by 85333da53SGreg Roach * the Free Software Foundation, either version 3 of the License, or 95333da53SGreg Roach * (at your option) any later version. 105333da53SGreg Roach * This program is distributed in the hope that it will be useful, 115333da53SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 125333da53SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 135333da53SGreg Roach * GNU General Public License for more details. 145333da53SGreg Roach * You should have received a copy of the GNU General Public License 155333da53SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 165333da53SGreg Roach */ 175333da53SGreg Roach 185333da53SGreg Roachdeclare(strict_types=1); 195333da53SGreg Roach 205333da53SGreg Roachnamespace Fisharebest\Webtrees; 215333da53SGreg Roach 225333da53SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService; 235333da53SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 245333da53SGreg Roachuse Illuminate\Support\Collection; 255333da53SGreg Roachuse stdClass; 265333da53SGreg Roach 27*79de923dSGreg Roachuse function app; 28*79de923dSGreg Roachuse function max; 29*79de923dSGreg Roachuse function min; 30*79de923dSGreg Roachuse function preg_split; 31*79de923dSGreg Roach 325333da53SGreg Roach/** 335333da53SGreg Roach * Class PlaceLocation 345333da53SGreg Roach */ 355333da53SGreg Roachclass PlaceLocation 365333da53SGreg Roach{ 375333da53SGreg Roach /** @var string e.g. "Westminster, London, England" */ 385333da53SGreg Roach private $location_name; 395333da53SGreg Roach 405333da53SGreg Roach /** @var Collection|string[] The parts of a location name, e.g. ["Westminster", "London", "England"] */ 415333da53SGreg Roach private $parts; 425333da53SGreg Roach 435333da53SGreg Roach /** 445333da53SGreg Roach * Create a place-location. 455333da53SGreg Roach * 465333da53SGreg Roach * @param string $location_name 475333da53SGreg Roach */ 485333da53SGreg Roach public function __construct(string $location_name) 495333da53SGreg Roach { 505333da53SGreg Roach // Ignore any empty parts in location names such as "Village, , , Country". 515333da53SGreg Roach $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name))) 525333da53SGreg Roach ->filter(); 535333da53SGreg Roach 545333da53SGreg Roach // Rebuild the location name in the correct format. 555333da53SGreg Roach $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 565333da53SGreg Roach } 575333da53SGreg Roach 585333da53SGreg Roach /** 595333da53SGreg Roach * Get the higher level location. 605333da53SGreg Roach * 615333da53SGreg Roach * @return PlaceLocation 625333da53SGreg Roach */ 635333da53SGreg Roach public function parent(): PlaceLocation 645333da53SGreg Roach { 655333da53SGreg Roach return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR)); 665333da53SGreg Roach } 675333da53SGreg Roach 685333da53SGreg Roach /** 695333da53SGreg Roach * The database row that contains this location. 705333da53SGreg Roach * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 715333da53SGreg Roach * 725333da53SGreg Roach * @return int 735333da53SGreg Roach */ 745333da53SGreg Roach public function id(): int 755333da53SGreg Roach { 765333da53SGreg Roach return app('cache.array')->remember('location-' . $this->location_name, function () { 775333da53SGreg Roach // The "top-level" location won't exist in the database. 785333da53SGreg Roach if ($this->parts->isEmpty()) { 795333da53SGreg Roach return 0; 805333da53SGreg Roach } 815333da53SGreg Roach 825333da53SGreg Roach $parent_location_id = $this->parent()->id(); 835333da53SGreg Roach 845333da53SGreg Roach $location_id = (int) DB::table('placelocation') 855333da53SGreg Roach ->where('pl_place', '=', $this->parts->first()) 865333da53SGreg Roach ->where('pl_parent_id', '=', $parent_location_id) 875333da53SGreg Roach ->value('pl_id'); 885333da53SGreg Roach 895333da53SGreg Roach if ($location_id === 0) { 905333da53SGreg Roach $location = $this->parts->first(); 915333da53SGreg Roach 925333da53SGreg Roach $location_id = 1 + (int) DB::table('placelocation')->max('pl_id'); 935333da53SGreg Roach 945333da53SGreg Roach DB::table('placelocation')->insert([ 955333da53SGreg Roach 'pl_id' => $location_id, 965333da53SGreg Roach 'pl_place' => $location, 975333da53SGreg Roach 'pl_parent_id' => $parent_location_id, 985333da53SGreg Roach 'pl_level' => $this->parts->count() - 1, 995333da53SGreg Roach 'pl_lati' => '', 1005333da53SGreg Roach 'pl_long' => '', 1015333da53SGreg Roach 'pl_icon' => '', 1025333da53SGreg Roach 'pl_zoom' => 2, 1035333da53SGreg Roach ]); 1045333da53SGreg Roach } 1055333da53SGreg Roach 1065333da53SGreg Roach return $location_id; 1075333da53SGreg Roach }); 1085333da53SGreg Roach } 1095333da53SGreg Roach 1105333da53SGreg Roach /** 1115333da53SGreg Roach * Does this location exist in the database? Note that calls to PlaceLocation::id() will 1125333da53SGreg Roach * create the row, so this function is only meaningful when called before a call to PlaceLocation::id(). 1135333da53SGreg Roach * 1145333da53SGreg Roach * @return bool 1155333da53SGreg Roach */ 1165333da53SGreg Roach public function exists(): bool 1175333da53SGreg Roach { 1185333da53SGreg Roach $location_id = 0; 1195333da53SGreg Roach 1205333da53SGreg Roach $this->parts->reverse()->each(static function (string $place) use (&$location_id) { 1215333da53SGreg Roach if ($location_id !== null) { 1225333da53SGreg Roach $location_id = DB::table('placelocation') 1235333da53SGreg Roach ->where('pl_parent_id', '=', $location_id) 1245333da53SGreg Roach ->where('pl_place', '=', $place) 1255333da53SGreg Roach ->value('pl_id'); 1265333da53SGreg Roach } 1275333da53SGreg Roach }); 1285333da53SGreg Roach 1295333da53SGreg Roach return $location_id !== null; 1305333da53SGreg Roach } 1315333da53SGreg Roach 1325333da53SGreg Roach /** 1335333da53SGreg Roach * @return stdClass 1345333da53SGreg Roach */ 1355333da53SGreg Roach private function details(): stdClass 1365333da53SGreg Roach { 1375333da53SGreg Roach return app('cache.array')->remember('location-details-' . $this->id(), function () { 1385333da53SGreg Roach // The "top-level" location won't exist in the database. 1395333da53SGreg Roach if ($this->parts->isEmpty()) { 1405333da53SGreg Roach return (object) [ 1415333da53SGreg Roach 'pl_id' => '0', 1425333da53SGreg Roach 'pl_parent_id' => '0', 1435333da53SGreg Roach 'pl_level' => null, 1445333da53SGreg Roach 'pl_place' => '', 1455333da53SGreg Roach 'pl_lati' => null, 1465333da53SGreg Roach 'pl_long' => null, 1475333da53SGreg Roach 'pl_zoom' => null, 1485333da53SGreg Roach 'pl_icon' => null, 1495333da53SGreg Roach 'pl_media' => null, 1505333da53SGreg Roach ]; 1515333da53SGreg Roach } 1525333da53SGreg Roach 1535333da53SGreg Roach return DB::table('placelocation') 1545333da53SGreg Roach ->where('pl_id', '=', $this->id()) 1555333da53SGreg Roach ->first(); 1565333da53SGreg Roach }); 1575333da53SGreg Roach } 1585333da53SGreg Roach 1595333da53SGreg Roach /** 1605333da53SGreg Roach * Latitude of the location. 1615333da53SGreg Roach * 1625333da53SGreg Roach * @return float 1635333da53SGreg Roach */ 1645333da53SGreg Roach public function latitude(): float 1655333da53SGreg Roach { 1665333da53SGreg Roach $gedcom_service = new GedcomService(); 1675333da53SGreg Roach 1685333da53SGreg Roach return $gedcom_service->readLatitude($this->details()->pl_lati ?? ''); 1695333da53SGreg Roach } 1705333da53SGreg Roach 1715333da53SGreg Roach /** 1725333da53SGreg Roach * Latitude of the longitude. 1735333da53SGreg Roach * 1745333da53SGreg Roach * @return float 1755333da53SGreg Roach */ 1765333da53SGreg Roach public function longitude(): float 1775333da53SGreg Roach { 1785333da53SGreg Roach $gedcom_service = new GedcomService(); 1795333da53SGreg Roach 1805333da53SGreg Roach return $gedcom_service->readLongitude($this->details()->pl_long ?? ''); 1815333da53SGreg Roach } 1825333da53SGreg Roach 1835333da53SGreg Roach /** 1845333da53SGreg Roach * The icon for the location. 1855333da53SGreg Roach * 1865333da53SGreg Roach * @return string 1875333da53SGreg Roach */ 1885333da53SGreg Roach public function icon(): string 1895333da53SGreg Roach { 1905333da53SGreg Roach return (string) $this->details()->pl_icon; 1915333da53SGreg Roach } 1925333da53SGreg Roach 1935333da53SGreg Roach /** 1945333da53SGreg Roach * Zoom level for the location. 1955333da53SGreg Roach * 1965333da53SGreg Roach * @return int 1975333da53SGreg Roach */ 1985333da53SGreg Roach public function zoom(): int 1995333da53SGreg Roach { 2005333da53SGreg Roach return (int) $this->details()->pl_zoom; 2015333da53SGreg Roach } 2025333da53SGreg Roach 2035333da53SGreg Roach /** 2045333da53SGreg Roach * @return string 2055333da53SGreg Roach */ 2065333da53SGreg Roach public function locationName(): string 2075333da53SGreg Roach { 2085333da53SGreg Roach return (string) $this->parts->first(); 2095333da53SGreg Roach } 210*79de923dSGreg Roach 211*79de923dSGreg Roach /** 212*79de923dSGreg Roach * Find a rectangle that (approximately) encloses this place. 213*79de923dSGreg Roach * 214*79de923dSGreg Roach * @return array<array<float>> 215*79de923dSGreg Roach */ 216*79de923dSGreg Roach public function boundingRectangle(): array 217*79de923dSGreg Roach { 218*79de923dSGreg Roach $latitude = $this->latitude(); 219*79de923dSGreg Roach $longitude = $this->longitude(); 220*79de923dSGreg Roach 221*79de923dSGreg Roach // No co-ordinates for this place? Try the parent place. 222*79de923dSGreg Roach $tmp = $this; 223*79de923dSGreg Roach while ($latitude === 0.0 && $longitude === 0.0 && $tmp->id() !== 0) { 224*79de923dSGreg Roach $tmp = $tmp->parent(); 225*79de923dSGreg Roach $latitude = $tmp->latitude(); 226*79de923dSGreg Roach $longitude = $tmp->longitude(); 227*79de923dSGreg Roach } 228*79de923dSGreg Roach 229*79de923dSGreg Roach // Show this number of degrees around the centre. 230*79de923dSGreg Roach if ($latitude === 0.0 && $longitude === 0.0 || $this->id() === 0) { 231*79de923dSGreg Roach // World 232*79de923dSGreg Roach $degrees = 180.0; 233*79de923dSGreg Roach } elseif ($this->parts->count() === 1) { 234*79de923dSGreg Roach // Countries 235*79de923dSGreg Roach $degrees = 10.0; 236*79de923dSGreg Roach } elseif ($this->parts->count() === 2) { 237*79de923dSGreg Roach // Regions 238*79de923dSGreg Roach $degrees = 3.0; 239*79de923dSGreg Roach } else { 240*79de923dSGreg Roach // Towns, cities and villages 241*79de923dSGreg Roach $degrees = 1.0; 242*79de923dSGreg Roach } 243*79de923dSGreg Roach 244*79de923dSGreg Roach return [ 245*79de923dSGreg Roach [ 246*79de923dSGreg Roach max($latitude - $degrees, -90.0), 247*79de923dSGreg Roach max($longitude - $degrees, -180.0), 248*79de923dSGreg Roach ], 249*79de923dSGreg Roach [ 250*79de923dSGreg Roach min($latitude + $degrees, 90.0), 251*79de923dSGreg Roach min($longitude + $degrees, 180.0), 252*79de923dSGreg Roach ], 253*79de923dSGreg Roach ]; 254*79de923dSGreg Roach } 2555333da53SGreg Roach} 256