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 2779de923dSGreg Roachuse function app; 2879de923dSGreg Roachuse function max; 2979de923dSGreg Roachuse function min; 3079de923dSGreg Roachuse function preg_split; 3179de923dSGreg 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 168e340fe65SGreg Roach $tmp = $this; 169e340fe65SGreg Roach do { 170e340fe65SGreg Roach $pl_lati = (string) $tmp->details()->pl_lati; 171e340fe65SGreg Roach $tmp = $tmp->parent(); 172e340fe65SGreg Roach } while ($pl_lati === '' && $tmp->id() !== 0); 173e340fe65SGreg Roach 174e340fe65SGreg Roach return $gedcom_service->readLatitude($pl_lati); 1755333da53SGreg Roach } 1765333da53SGreg Roach 1775333da53SGreg Roach /** 1785333da53SGreg Roach * Latitude of the longitude. 1795333da53SGreg Roach * 1805333da53SGreg Roach * @return float 1815333da53SGreg Roach */ 1825333da53SGreg Roach public function longitude(): float 1835333da53SGreg Roach { 1845333da53SGreg Roach $gedcom_service = new GedcomService(); 1855333da53SGreg Roach 186e340fe65SGreg Roach $tmp = $this; 187e340fe65SGreg Roach do { 188e340fe65SGreg Roach $pl_long = (string) $tmp->details()->pl_long; 189e340fe65SGreg Roach $tmp = $tmp->parent(); 190e340fe65SGreg Roach } while ($pl_long === '' && $tmp->id() !== 0); 191e340fe65SGreg Roach 192e340fe65SGreg Roach return $gedcom_service->readLongitude($pl_long); 1935333da53SGreg Roach } 1945333da53SGreg Roach 1955333da53SGreg Roach /** 1965333da53SGreg Roach * The icon for the location. 1975333da53SGreg Roach * 1985333da53SGreg Roach * @return string 1995333da53SGreg Roach */ 2005333da53SGreg Roach public function icon(): string 2015333da53SGreg Roach { 2025333da53SGreg Roach return (string) $this->details()->pl_icon; 2035333da53SGreg Roach } 2045333da53SGreg Roach 2055333da53SGreg Roach /** 2065333da53SGreg Roach * Zoom level for the location. 2075333da53SGreg Roach * 2085333da53SGreg Roach * @return int 2095333da53SGreg Roach */ 2105333da53SGreg Roach public function zoom(): int 2115333da53SGreg Roach { 212e340fe65SGreg Roach return (int) $this->details()->pl_zoom ?: 2; 2135333da53SGreg Roach } 2145333da53SGreg Roach 2155333da53SGreg Roach /** 2165333da53SGreg Roach * @return string 2175333da53SGreg Roach */ 2185333da53SGreg Roach public function locationName(): string 2195333da53SGreg Roach { 2205333da53SGreg Roach return (string) $this->parts->first(); 2215333da53SGreg Roach } 22279de923dSGreg Roach 22379de923dSGreg Roach /** 22479de923dSGreg Roach * Find a rectangle that (approximately) encloses this place. 22579de923dSGreg Roach * 22679de923dSGreg Roach * @return array<array<float>> 22779de923dSGreg Roach */ 22879de923dSGreg Roach public function boundingRectangle(): array 22979de923dSGreg Roach { 230*9a9dfcf7SGreg Roach if ($this->id() === 0) { 231*9a9dfcf7SGreg Roach return [[-180.0, -90.0], [180.0, 90.0]]; 23279de923dSGreg Roach } 23379de923dSGreg Roach 234*9a9dfcf7SGreg Roach // Find our own co-ordinates and those of any child places 235*9a9dfcf7SGreg Roach $latitudes = DB::table('placelocation') 236*9a9dfcf7SGreg Roach ->where('pl_parent_id', '=', $this->id()) 237*9a9dfcf7SGreg Roach ->orWhere('pl_id', '=', $this->id()) 238*9a9dfcf7SGreg Roach ->pluck('pl_lati') 239*9a9dfcf7SGreg Roach ->filter() 240*9a9dfcf7SGreg Roach ->map(static function (string $x): float { 241*9a9dfcf7SGreg Roach return (new GedcomService())->readLatitude($x); 242*9a9dfcf7SGreg Roach }); 243*9a9dfcf7SGreg Roach 244*9a9dfcf7SGreg Roach $longitudes = DB::table('placelocation') 245*9a9dfcf7SGreg Roach ->where('pl_parent_id', '=', $this->id()) 246*9a9dfcf7SGreg Roach ->orWhere('pl_id', '=', $this->id()) 247*9a9dfcf7SGreg Roach ->pluck('pl_long') 248*9a9dfcf7SGreg Roach ->filter() 249*9a9dfcf7SGreg Roach ->map(static function (string $x): float { 250*9a9dfcf7SGreg Roach return (new GedcomService())->readLongitude($x); 251*9a9dfcf7SGreg Roach }); 252*9a9dfcf7SGreg Roach 253*9a9dfcf7SGreg Roach if ($latitudes->count() > 1 || $longitudes->count() > 1) { 254*9a9dfcf7SGreg Roach return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]]; 25579de923dSGreg Roach } 25679de923dSGreg Roach 257*9a9dfcf7SGreg Roach return $this->parent()->boundingRectangle(); 25879de923dSGreg Roach } 2595333da53SGreg Roach} 260