1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20use Fisharebest\Webtrees\Services\GedcomService; 21use Illuminate\Database\Capsule\Manager as DB; 22use Illuminate\Support\Collection; 23use stdClass; 24 25/** 26 * Class Location 27 */ 28class Location 29{ 30 /** @var string e.g. "Westminster, London, England" */ 31 private $location_name; 32 33 /** @var Collection|string[] The parts of a location name, e.g. ["Westminster", "London", "England"] */ 34 private $parts; 35 36 /** 37 * Create a location. 38 * 39 * @param string $location_name 40 */ 41 public function __construct(string $location_name) 42 { 43 // Ignore any empty parts in location names such as "Village, , , Country". 44 $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name))) 45 ->filter(); 46 47 // Rebuild the location name in the correct format. 48 $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 49 } 50 51 /** 52 * Get the higher level location. 53 * 54 * @return Location 55 */ 56 public function parent(): Location 57 { 58 return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR)); 59 } 60 61 /** 62 * The database row that contains this location. 63 * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 64 * 65 * @return int 66 */ 67 public function id(): int 68 { 69 return app()->make('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->location_name, function () { 70 // The "top-level" location won't exist in the database. 71 if ($this->parts->isEmpty()) { 72 return 0; 73 } 74 75 $parent_location_id = $this->parent()->id(); 76 77 $location_id = (int) DB::table('placelocation') 78 ->where('pl_place', '=', $this->parts->first()) 79 ->where('pl_parent_id', '=', $parent_location_id) 80 ->value('pl_id'); 81 82 if ($location_id === 0) { 83 $location = $this->parts->first(); 84 85 $location_id = 1 + (int) DB::table('placelocation')->max('pl_id'); 86 87 DB::table('placelocation')->insert([ 88 'pl_id' => $location_id, 89 'pl_place' => $location, 90 'pl_parent_id' => $parent_location_id, 91 'pl_level' => $this->parts->count() - 1, 92 ]); 93 } 94 95 return $location_id; 96 }); 97 } 98 99 /** 100 * Does this location exist in the database? Note that calls to Location::id() will 101 * create the row, so this function is only meaningful when called before a call to Location::id(). 102 * 103 * @return bool 104 */ 105 public function exists(): bool 106 { 107 $location_id = 0; 108 109 $this->parts->reverse()->each(function (string $place) use (&$location_id) { 110 if ($location_id !== null) { 111 $location_id = DB::table('placelocation') 112 ->where('pl_parent_id', '=', $location_id) 113 ->where('pl_place', '=', $place) 114 ->value('pl_id'); 115 } 116 }); 117 118 return $location_id !== null; 119 } 120 121 /** 122 * @return stdClass 123 */ 124 private function details(): stdClass 125 { 126 return app()->make('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->id(), function () { 127 // The "top-level" location won't exist in the database. 128 if ($this->parts->isEmpty()) { 129 return (object) [ 130 'pl_id' => '0', 131 'pl_parent_id' => '0', 132 'pl_level' => null, 133 'pl_place' => '', 134 'pl_lati' => null, 135 'pl_long' => null, 136 'pl_zoom' => null, 137 'pl_icon' => null, 138 'pl_media' => null, 139 ]; 140 } 141 142 return DB::table('placelocation') 143 ->where('pl_id', '=', $this->id()) 144 ->first(); 145 }); 146 } 147 148 /** 149 * Latitude of the location. 150 * 151 * @return float 152 */ 153 public function latitude(): float 154 { 155 $gedcom_service = new GedcomService(); 156 157 return $gedcom_service->readLatitude($this->details()->pl_lati ?? ''); 158 } 159 160 /** 161 * Latitude of the longitude. 162 * 163 * @return float 164 */ 165 public function longitude(): float 166 { 167 $gedcom_service = new GedcomService(); 168 169 return $gedcom_service->readLongitude($this->details()->pl_long ?? ''); 170 } 171 172 /** 173 * The icon for the location. 174 * 175 * @return string 176 */ 177 public function icon(): string 178 { 179 return (string) $this->details()->pl_icon; 180 } 181 182 /** 183 * Zoom level for the location. 184 * 185 * @return int 186 */ 187 public function zoom(): int 188 { 189 return (int) $this->details()->pl_zoom; 190 } 191 192 /** 193 * @return string 194 */ 195 public function locationName(): string 196 { 197 return (string) $this->parts->first(); 198 } 199} 200