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|string[] 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()->make('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->place_name, function () { 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)->findByComponent(ModuleListInterface::class, $this->tree, Auth::user())->first(function (ModuleInterface $module) { 166 return $module instanceof PlaceHierarchyListModule; 167 }); 168 169 if ($module instanceof PlaceHierarchyListModule) { 170 return $module->listUrl($this->tree, [ 171 'parent' => $this->parts->reverse()->all(), 172 'ged' => $this->tree->name(), 173 ]); 174 } else { 175 //TODO: should we be allowed to return null here? 176 return \Fisharebest\Webtrees\Html::url('index.php', []); 177 } 178 } 179 180 /** 181 * Format this place for GEDCOM data. 182 * 183 * @return string 184 */ 185 public function gedcomName(): string 186 { 187 return $this->place_name; 188 } 189 190 /** 191 * Format this place for display on screen. 192 * 193 * @return string 194 */ 195 public function placeName(): string 196 { 197 $place_name = $this->parts->first() ?? I18N::translate('unknown'); 198 199 return '<span dir="auto">' . e($place_name) . '</span>'; 200 } 201 202 /** 203 * Generate the place name for display, including the full hierarchy. 204 * 205 * @param bool $link 206 * 207 * @return string 208 */ 209 public function fullName(bool $link = false) 210 { 211 if ($this->parts->isEmpty()) { 212 return ''; 213 } 214 215 $full_name = $this->parts->implode(I18N::$list_separator); 216 217 if ($link) { 218 return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>'; 219 } 220 221 return '<span dir="auto">' . e($full_name) . '</span>'; 222 } 223 224 /** 225 * For lists and charts, where the full name won’t fit. 226 * 227 * @param bool $link 228 * 229 * @return string 230 */ 231 public function shortName(bool $link = false) 232 { 233 $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); 234 235 // Abbreviate the place name, for lists 236 if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { 237 $parts = $this->lastParts($SHOW_PEDIGREE_PLACES); 238 } else { 239 $parts = $this->firstParts($SHOW_PEDIGREE_PLACES); 240 } 241 242 $short_name = $parts->implode(I18N::$list_separator); 243 244 // Add a tool-tip showing the full name 245 $title = strip_tags($this->fullName()); 246 247 if ($link) { 248 return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '"">' . e($short_name) . '</a>'; 249 } 250 251 return '<span dir="auto">' . e($short_name) . '</span>'; 252 } 253} 254