1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a25f0a04SGreg Roach * (at your option) any later version. 10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a25f0a04SGreg Roach * GNU General Public License for more details. 14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees; 21a25f0a04SGreg Roach 2267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\ModuleInterface; 2387cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface; 2467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule; 2567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 26b68caec6SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 27a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 28392561bbSGreg Roachuse Illuminate\Support\Collection; 29b68caec6SGreg Roach 30f70bcff5SGreg Roachuse function app; 31f70bcff5SGreg Roachuse function e; 32f70bcff5SGreg Roachuse function is_object; 33f70bcff5SGreg Roachuse function preg_split; 34f70bcff5SGreg Roachuse function strip_tags; 3590949315SGreg Roachuse function trim; 3690949315SGreg Roach 3790949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY; 3890949315SGreg Roach 39a25f0a04SGreg Roach/** 4076692c8bSGreg Roach * A GEDCOM place (PLAC) object. 41a25f0a04SGreg Roach */ 42c1010edaSGreg Roachclass Place 43c1010edaSGreg Roach{ 4443f2f523SGreg Roach // "Westminster, London, England" 4543f2f523SGreg Roach private string $place_name; 46392561bbSGreg Roach 4736779af1SGreg Roach /** @var Collection<int,string> The parts of a place name, e.g. ["Westminster", "London", "England"] */ 4843f2f523SGreg Roach private Collection $parts; 4984caa210SGreg Roach 5043f2f523SGreg Roach private Tree $tree; 51a25f0a04SGreg Roach 52a25f0a04SGreg Roach /** 5376692c8bSGreg Roach * Create a place. 5476692c8bSGreg Roach * 55392561bbSGreg Roach * @param string $place_name 5684caa210SGreg Roach * @param Tree $tree 57a25f0a04SGreg Roach */ 58392561bbSGreg Roach public function __construct(string $place_name, Tree $tree) 59c1010edaSGreg Roach { 60392561bbSGreg Roach // Ignore any empty parts in place names such as "Village, , , Country". 6190949315SGreg Roach $place_name = trim($place_name); 6290949315SGreg Roach $this->parts = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name, -1, PREG_SPLIT_NO_EMPTY)); 63392561bbSGreg Roach 64392561bbSGreg Roach // Rebuild the placename in the correct format. 65392561bbSGreg Roach $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 66392561bbSGreg Roach 6764b16745SGreg Roach $this->tree = $tree; 68a25f0a04SGreg Roach } 69a25f0a04SGreg Roach 70a25f0a04SGreg Roach /** 717bed239cSGreg Roach * Find a place by its ID. 727bed239cSGreg Roach * 737bed239cSGreg Roach * @param int $id 747bed239cSGreg Roach * @param Tree $tree 757bed239cSGreg Roach * 767bed239cSGreg Roach * @return Place 777bed239cSGreg Roach */ 787bed239cSGreg Roach public static function find(int $id, Tree $tree): Place 797bed239cSGreg Roach { 807bed239cSGreg Roach $parts = new Collection(); 817bed239cSGreg Roach 827bed239cSGreg Roach while ($id !== 0) { 837bed239cSGreg Roach $row = DB::table('places') 847bed239cSGreg Roach ->where('p_file', '=', $tree->id()) 857bed239cSGreg Roach ->where('p_id', '=', $id) 867bed239cSGreg Roach ->first(); 877bed239cSGreg Roach 88f70bcff5SGreg Roach if (is_object($row)) { 897bed239cSGreg Roach $id = (int) $row->p_parent_id; 907bed239cSGreg Roach $parts->add($row->p_place); 917bed239cSGreg Roach } else { 927bed239cSGreg Roach $id = 0; 937bed239cSGreg Roach } 947bed239cSGreg Roach } 957bed239cSGreg Roach 967bed239cSGreg Roach $place_name = $parts->implode(Gedcom::PLACE_SEPARATOR); 977bed239cSGreg Roach 987bed239cSGreg Roach return new Place($place_name, $tree); 997bed239cSGreg Roach } 1007bed239cSGreg Roach 1017bed239cSGreg Roach /** 10276692c8bSGreg Roach * Get the higher level place. 10376692c8bSGreg Roach * 104a25f0a04SGreg Roach * @return Place 105a25f0a04SGreg Roach */ 106392561bbSGreg Roach public function parent(): Place 107c1010edaSGreg Roach { 1088af6bbf8SGreg Roach return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree); 109392561bbSGreg Roach } 110392561bbSGreg Roach 111392561bbSGreg Roach /** 112392561bbSGreg Roach * The database row that contains this place. 113392561bbSGreg Roach * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 114392561bbSGreg Roach * 115392561bbSGreg Roach * @return int 116392561bbSGreg Roach */ 117392561bbSGreg Roach public function id(): int 118392561bbSGreg Roach { 1196b9cb339SGreg Roach return Registry::cache()->array()->remember('place-' . $this->place_name, function (): int { 120392561bbSGreg Roach // The "top-level" place won't exist in the database. 121392561bbSGreg Roach if ($this->parts->isEmpty()) { 122392561bbSGreg Roach return 0; 123392561bbSGreg Roach } 124392561bbSGreg Roach 1258af6bbf8SGreg Roach $parent_place_id = $this->parent()->id(); 126392561bbSGreg Roach 127392561bbSGreg Roach $place_id = (int) DB::table('places') 128392561bbSGreg Roach ->where('p_file', '=', $this->tree->id()) 129c4ab7783SGreg Roach ->where('p_place', '=', mb_substr($this->parts->first(), 0, 120)) 1308af6bbf8SGreg Roach ->where('p_parent_id', '=', $parent_place_id) 131392561bbSGreg Roach ->value('p_id'); 132392561bbSGreg Roach 133392561bbSGreg Roach if ($place_id === 0) { 134392561bbSGreg Roach $place = $this->parts->first(); 135392561bbSGreg Roach 136392561bbSGreg Roach DB::table('places')->insert([ 137392561bbSGreg Roach 'p_file' => $this->tree->id(), 138c4ab7783SGreg Roach 'p_place' => mb_substr($place, 0, 120), 1398af6bbf8SGreg Roach 'p_parent_id' => $parent_place_id, 140392561bbSGreg Roach 'p_std_soundex' => Soundex::russell($place), 141392561bbSGreg Roach 'p_dm_soundex' => Soundex::daitchMokotoff($place), 142392561bbSGreg Roach ]); 143392561bbSGreg Roach 144392561bbSGreg Roach $place_id = (int) DB::connection()->getPdo()->lastInsertId(); 145392561bbSGreg Roach } 146392561bbSGreg Roach 147392561bbSGreg Roach return $place_id; 148392561bbSGreg Roach }); 149392561bbSGreg Roach } 150392561bbSGreg Roach 151392561bbSGreg Roach /** 152dbe53437SGreg Roach * @return Tree 153dbe53437SGreg Roach */ 154dbe53437SGreg Roach public function tree(): Tree 155dbe53437SGreg Roach { 156dbe53437SGreg Roach return $this->tree; 157dbe53437SGreg Roach } 158dbe53437SGreg Roach 159dbe53437SGreg Roach /** 160392561bbSGreg Roach * Extract the locality (first parts) of a place name. 161392561bbSGreg Roach * 162392561bbSGreg Roach * @param int $n 163392561bbSGreg Roach * 16436779af1SGreg Roach * @return Collection<int,string> 165392561bbSGreg Roach */ 166392561bbSGreg Roach public function firstParts(int $n): Collection 167392561bbSGreg Roach { 168392561bbSGreg Roach return $this->parts->slice(0, $n); 169392561bbSGreg Roach } 170392561bbSGreg Roach 171392561bbSGreg Roach /** 172392561bbSGreg Roach * Extract the country (last parts) of a place name. 173392561bbSGreg Roach * 174392561bbSGreg Roach * @param int $n 175392561bbSGreg Roach * 17636779af1SGreg Roach * @return Collection<int,string> 177392561bbSGreg Roach */ 178392561bbSGreg Roach public function lastParts(int $n): Collection 179392561bbSGreg Roach { 180392561bbSGreg Roach return $this->parts->slice(-$n); 181a25f0a04SGreg Roach } 182a25f0a04SGreg Roach 183a25f0a04SGreg Roach /** 18476692c8bSGreg Roach * Get the lower level places. 18576692c8bSGreg Roach * 186d7634abbSGreg Roach * @return array<Place> 187a25f0a04SGreg Roach */ 1888f53f488SRico Sonntag public function getChildPlaces(): array 189c1010edaSGreg Roach { 190392561bbSGreg Roach if ($this->place_name !== '') { 191392561bbSGreg Roach $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name; 192a25f0a04SGreg Roach } else { 193a25f0a04SGreg Roach $parent_text = ''; 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach 196b68caec6SGreg Roach return DB::table('places') 197b68caec6SGreg Roach ->where('p_file', '=', $this->tree->id()) 198392561bbSGreg Roach ->where('p_parent_id', '=', $this->id()) 199a69f5655SGreg Roach ->orderBy(new Expression('p_place /*! COLLATE ' . I18N::collation() . ' */')) 200b68caec6SGreg Roach ->pluck('p_place') 201b68caec6SGreg Roach ->map(function (string $place) use ($parent_text): Place { 202b68caec6SGreg Roach return new self($place . $parent_text, $this->tree); 203b68caec6SGreg Roach }) 204b68caec6SGreg Roach ->all(); 205a25f0a04SGreg Roach } 206a25f0a04SGreg Roach 207a25f0a04SGreg Roach /** 20876692c8bSGreg Roach * Create a URL to the place-hierarchy page. 20976692c8bSGreg Roach * 210a25f0a04SGreg Roach * @return string 211a25f0a04SGreg Roach */ 212e8777cb5SGreg Roach public function url(): string 213c1010edaSGreg Roach { 21467992b6aSRichard Cissee //find a module providing the place hierarchy 2150797053bSGreg Roach $module = app(ModuleService::class) 2160797053bSGreg Roach ->findByComponent(ModuleListInterface::class, $this->tree, Auth::user()) 2170797053bSGreg Roach ->first(static function (ModuleInterface $module): bool { 21867992b6aSRichard Cissee return $module instanceof PlaceHierarchyListModule; 21967992b6aSRichard Cissee }); 22067992b6aSRichard Cissee 22167992b6aSRichard Cissee if ($module instanceof PlaceHierarchyListModule) { 22267992b6aSRichard Cissee return $module->listUrl($this->tree, [ 2237bed239cSGreg Roach 'place_id' => $this->id(), 2249022ab66SGreg Roach 'tree' => $this->tree->name(), 225e8777cb5SGreg Roach ]); 226e364afe4SGreg Roach } 227e364afe4SGreg Roach 228f7721877SGreg Roach // The place-list module is disabled... 229f7721877SGreg Roach return '#'; 23067992b6aSRichard Cissee } 231a25f0a04SGreg Roach 232a25f0a04SGreg Roach /** 233392561bbSGreg Roach * Format this place for GEDCOM data. 23476692c8bSGreg Roach * 235a25f0a04SGreg Roach * @return string 236a25f0a04SGreg Roach */ 237392561bbSGreg Roach public function gedcomName(): string 238c1010edaSGreg Roach { 239392561bbSGreg Roach return $this->place_name; 240a25f0a04SGreg Roach } 241a25f0a04SGreg Roach 242a25f0a04SGreg Roach /** 24376692c8bSGreg Roach * Format this place for display on screen. 24476692c8bSGreg Roach * 245a25f0a04SGreg Roach * @return string 246a25f0a04SGreg Roach */ 247392561bbSGreg Roach public function placeName(): string 248c1010edaSGreg Roach { 249392561bbSGreg Roach $place_name = $this->parts->first() ?? I18N::translate('unknown'); 250a25f0a04SGreg Roach 251315eb316SGreg Roach return '<bdi>' . e($place_name) . '</bdi>'; 252a25f0a04SGreg Roach } 253a25f0a04SGreg Roach 254a25f0a04SGreg Roach /** 25576692c8bSGreg Roach * Generate the place name for display, including the full hierarchy. 25676692c8bSGreg Roach * 257392561bbSGreg Roach * @param bool $link 258392561bbSGreg Roach * 259a25f0a04SGreg Roach * @return string 260a25f0a04SGreg Roach */ 261e364afe4SGreg Roach public function fullName(bool $link = false): string 262c1010edaSGreg Roach { 263392561bbSGreg Roach if ($this->parts->isEmpty()) { 264392561bbSGreg Roach return ''; 265b2ce94c6SRico Sonntag } 266b2ce94c6SRico Sonntag 267392561bbSGreg Roach $full_name = $this->parts->implode(I18N::$list_separator); 268392561bbSGreg Roach 269392561bbSGreg Roach if ($link) { 270392561bbSGreg Roach return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>'; 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach 273315eb316SGreg Roach return '<bdi>' . e($full_name) . '</bdi>'; 274a25f0a04SGreg Roach } 275a25f0a04SGreg Roach 276a25f0a04SGreg Roach /** 277a25f0a04SGreg Roach * For lists and charts, where the full name won’t fit. 278a25f0a04SGreg Roach * 279392561bbSGreg Roach * @param bool $link 280a25f0a04SGreg Roach * 281a25f0a04SGreg Roach * @return string 282a25f0a04SGreg Roach */ 283e364afe4SGreg Roach public function shortName(bool $link = false): string 284c1010edaSGreg Roach { 285392561bbSGreg Roach $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); 286392561bbSGreg Roach 287392561bbSGreg Roach // Abbreviate the place name, for lists 288392561bbSGreg Roach if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { 289392561bbSGreg Roach $parts = $this->lastParts($SHOW_PEDIGREE_PLACES); 290392561bbSGreg Roach } else { 291392561bbSGreg Roach $parts = $this->firstParts($SHOW_PEDIGREE_PLACES); 292a25f0a04SGreg Roach } 293a25f0a04SGreg Roach 294392561bbSGreg Roach $short_name = $parts->implode(I18N::$list_separator); 295392561bbSGreg Roach 296392561bbSGreg Roach // Add a tool-tip showing the full name 297392561bbSGreg Roach $title = strip_tags($this->fullName()); 298392561bbSGreg Roach 299392561bbSGreg Roach if ($link) { 30040f99dd2SGreg Roach return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '">' . e($short_name) . '</a>'; 301392561bbSGreg Roach } 302392561bbSGreg Roach 303315eb316SGreg Roach return '<bdi>' . e($short_name) . '</bdi>'; 304a25f0a04SGreg Roach } 305a25f0a04SGreg Roach} 306