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 Illuminate\Database\Capsule\Manager as DB; 21use Illuminate\Support\Collection; 22 23/** 24 * A GEDCOM place (PLAC) object. 25 */ 26class Place 27{ 28 /** @var string e.g. "Westminster, London, England" */ 29 private $place_name; 30 31 /** @var Collection|string[] The parts of a place name, e.g. ["Westminster", "London", "England"] */ 32 private $parts; 33 34 /** @var Tree We may have the same place name in different trees. */ 35 private $tree; 36 37 /** 38 * Create a place. 39 * 40 * @param string $place_name 41 * @param Tree $tree 42 */ 43 public function __construct(string $place_name, Tree $tree) 44 { 45 // Ignore any empty parts in place names such as "Village, , , Country". 46 $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name))) 47 ->filter(); 48 49 // Rebuild the placename in the correct format. 50 $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 51 52 $this->tree = $tree; 53 } 54 55 /** 56 * Get the higher level place. 57 * 58 * @return Place 59 */ 60 public function parent(): Place 61 { 62 return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree); 63 } 64 65 /** 66 * The database row that contains this place. 67 * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 68 * 69 * @return int 70 */ 71 public function id(): int 72 { 73 return app()->make('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->place_name, function () { 74 // The "top-level" place won't exist in the database. 75 if ($this->parts->isEmpty()) { 76 return 0; 77 } 78 79 $parent_place_id = $this->parent()->id(); 80 81 $place_id = (int) DB::table('places') 82 ->where('p_file', '=', $this->tree->id()) 83 ->where('p_place', '=', $this->parts->first()) 84 ->where('p_parent_id', '=', $parent_place_id) 85 ->value('p_id'); 86 87 if ($place_id === 0) { 88 $place = $this->parts->first(); 89 90 DB::table('places')->insert([ 91 'p_file' => $this->tree->id(), 92 'p_place' => $place, 93 'p_parent_id' => $parent_place_id, 94 'p_std_soundex' => Soundex::russell($place), 95 'p_dm_soundex' => Soundex::daitchMokotoff($place), 96 ]); 97 98 $place_id = (int) DB::connection()->getPdo()->lastInsertId(); 99 } 100 101 return $place_id; 102 }); 103 } 104 105 /** 106 * Extract the locality (first parts) of a place name. 107 * 108 * @param int $n 109 * 110 * @return Collection 111 */ 112 public function firstParts(int $n): Collection 113 { 114 return $this->parts->slice(0, $n); 115 } 116 117 /** 118 * Extract the country (last parts) of a place name. 119 * 120 * @param int $n 121 * 122 * @return Collection 123 */ 124 public function lastParts(int $n): Collection 125 { 126 return $this->parts->slice(-$n); 127 } 128 129 /** 130 * Get the lower level places. 131 * 132 * @return Place[] 133 */ 134 public function getChildPlaces(): array 135 { 136 if ($this->place_name !== '') { 137 $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name; 138 } else { 139 $parent_text = ''; 140 } 141 142 return DB::table('places') 143 ->where('p_file', '=', $this->tree->id()) 144 ->where('p_parent_id', '=', $this->id()) 145 ->orderBy(DB::raw('p_place /*! COLLATE ' . I18N::collation() . ' */')) 146 ->pluck('p_place') 147 ->map(function (string $place) use ($parent_text): Place { 148 return new self($place . $parent_text, $this->tree); 149 }) 150 ->all(); 151 } 152 153 /** 154 * Create a URL to the place-hierarchy page. 155 * 156 * @return string 157 */ 158 public function url(): string 159 { 160 return route('place-hierarchy', [ 161 'parent' => $this->parts->reverse()->all(), 162 'ged' => $this->tree->name(), 163 ]); 164 } 165 166 /** 167 * Format this place for GEDCOM data. 168 * 169 * @return string 170 */ 171 public function gedcomName(): string 172 { 173 return $this->place_name; 174 } 175 176 /** 177 * Format this place for display on screen. 178 * 179 * @return string 180 */ 181 public function placeName(): string 182 { 183 $place_name = $this->parts->first() ?? I18N::translate('unknown'); 184 185 return '<span dir="auto">' . e($place_name) . '</span>'; 186 } 187 188 /** 189 * Generate the place name for display, including the full hierarchy. 190 * 191 * @param bool $link 192 * 193 * @return string 194 */ 195 public function fullName(bool $link = false) 196 { 197 if ($this->parts->isEmpty()) { 198 return ''; 199 } 200 201 $full_name = $this->parts->implode(I18N::$list_separator); 202 203 if ($link) { 204 return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>'; 205 } 206 207 return '<span dir="auto">' . e($full_name) . '</span>'; 208 } 209 210 /** 211 * For lists and charts, where the full name won’t fit. 212 * 213 * @param bool $link 214 * 215 * @return string 216 */ 217 public function shortName(bool $link = false) 218 { 219 $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); 220 221 // Abbreviate the place name, for lists 222 if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { 223 $parts = $this->lastParts($SHOW_PEDIGREE_PLACES); 224 } else { 225 $parts = $this->firstParts($SHOW_PEDIGREE_PLACES); 226 } 227 228 $short_name = $parts->implode(I18N::$list_separator); 229 230 // Add a tool-tip showing the full name 231 $title = strip_tags($this->fullName()); 232 233 if ($link) { 234 return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '"">' . e($short_name) . '</a>'; 235 } 236 237 return '<span dir="auto">' . e($short_name) . '</span>'; 238 } 239} 240