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