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 27/** 28 * Class PlaceLocation 29 */ 30class PlaceLocation 31{ 32 /** @var string e.g. "Westminster, London, England" */ 33 private $location_name; 34 35 /** @var Collection|string[] The parts of a location name, e.g. ["Westminster", "London", "England"] */ 36 private $parts; 37 38 /** 39 * Create a place-location. 40 * 41 * @param string $location_name 42 */ 43 public function __construct(string $location_name) 44 { 45 // Ignore any empty parts in location names such as "Village, , , Country". 46 $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name))) 47 ->filter(); 48 49 // Rebuild the location name in the correct format. 50 $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 51 } 52 53 /** 54 * Get the higher level location. 55 * 56 * @return PlaceLocation 57 */ 58 public function parent(): PlaceLocation 59 { 60 return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR)); 61 } 62 63 /** 64 * The database row that contains this location. 65 * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 66 * 67 * @return int 68 */ 69 public function id(): int 70 { 71 return app('cache.array')->remember('location-' . $this->location_name, function () { 72 // The "top-level" location won't exist in the database. 73 if ($this->parts->isEmpty()) { 74 return 0; 75 } 76 77 $parent_location_id = $this->parent()->id(); 78 79 $location_id = (int) DB::table('placelocation') 80 ->where('pl_place', '=', $this->parts->first()) 81 ->where('pl_parent_id', '=', $parent_location_id) 82 ->value('pl_id'); 83 84 if ($location_id === 0) { 85 $location = $this->parts->first(); 86 87 $location_id = 1 + (int) DB::table('placelocation')->max('pl_id'); 88 89 DB::table('placelocation')->insert([ 90 'pl_id' => $location_id, 91 'pl_place' => $location, 92 'pl_parent_id' => $parent_location_id, 93 'pl_level' => $this->parts->count() - 1, 94 'pl_lati' => '', 95 'pl_long' => '', 96 'pl_icon' => '', 97 'pl_zoom' => 2, 98 ]); 99 } 100 101 return $location_id; 102 }); 103 } 104 105 /** 106 * Does this location exist in the database? Note that calls to PlaceLocation::id() will 107 * create the row, so this function is only meaningful when called before a call to PlaceLocation::id(). 108 * 109 * @return bool 110 */ 111 public function exists(): bool 112 { 113 $location_id = 0; 114 115 $this->parts->reverse()->each(static function (string $place) use (&$location_id) { 116 if ($location_id !== null) { 117 $location_id = DB::table('placelocation') 118 ->where('pl_parent_id', '=', $location_id) 119 ->where('pl_place', '=', $place) 120 ->value('pl_id'); 121 } 122 }); 123 124 return $location_id !== null; 125 } 126 127 /** 128 * @return stdClass 129 */ 130 private function details(): stdClass 131 { 132 return app('cache.array')->remember('location-details-' . $this->id(), function () { 133 // The "top-level" location won't exist in the database. 134 if ($this->parts->isEmpty()) { 135 return (object) [ 136 'pl_id' => '0', 137 'pl_parent_id' => '0', 138 'pl_level' => null, 139 'pl_place' => '', 140 'pl_lati' => null, 141 'pl_long' => null, 142 'pl_zoom' => null, 143 'pl_icon' => null, 144 'pl_media' => null, 145 ]; 146 } 147 148 return DB::table('placelocation') 149 ->where('pl_id', '=', $this->id()) 150 ->first(); 151 }); 152 } 153 154 /** 155 * Latitude of the location. 156 * 157 * @return float 158 */ 159 public function latitude(): float 160 { 161 $gedcom_service = new GedcomService(); 162 163 return $gedcom_service->readLatitude($this->details()->pl_lati ?? ''); 164 } 165 166 /** 167 * Latitude of the longitude. 168 * 169 * @return float 170 */ 171 public function longitude(): float 172 { 173 $gedcom_service = new GedcomService(); 174 175 return $gedcom_service->readLongitude($this->details()->pl_long ?? ''); 176 } 177 178 /** 179 * The icon for the location. 180 * 181 * @return string 182 */ 183 public function icon(): string 184 { 185 return (string) $this->details()->pl_icon; 186 } 187 188 /** 189 * Zoom level for the location. 190 * 191 * @return int 192 */ 193 public function zoom(): int 194 { 195 return (int) $this->details()->pl_zoom; 196 } 197 198 /** 199 * @return string 200 */ 201 public function locationName(): string 202 { 203 return (string) $this->parts->first(); 204 } 205} 206