1a25f0a04SGreg Roach<?php 23976b470SGreg Roach 3a25f0a04SGreg Roach/** 4a25f0a04SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 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 15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16a25f0a04SGreg Roach */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees; 20a25f0a04SGreg Roach 2167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\ModuleInterface; 2287cca37cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface; 2367992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule; 2467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService; 25b68caec6SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 26a69f5655SGreg Roachuse Illuminate\Database\Query\Expression; 27392561bbSGreg Roachuse Illuminate\Support\Collection; 28b68caec6SGreg Roach 29a25f0a04SGreg Roach/** 3076692c8bSGreg Roach * A GEDCOM place (PLAC) object. 31a25f0a04SGreg Roach */ 32c1010edaSGreg Roachclass Place 33c1010edaSGreg Roach{ 34392561bbSGreg Roach /** @var string e.g. "Westminster, London, England" */ 35392561bbSGreg Roach private $place_name; 36392561bbSGreg Roach 3725d7fe95SGreg Roach /** @var Collection The parts of a place name, e.g. ["Westminster", "London", "England"] */ 38392561bbSGreg Roach private $parts; 3984caa210SGreg Roach 4076692c8bSGreg Roach /** @var Tree We may have the same place name in different trees. */ 4184caa210SGreg Roach private $tree; 42a25f0a04SGreg Roach 43a25f0a04SGreg Roach /** 4476692c8bSGreg Roach * Create a place. 4576692c8bSGreg Roach * 46392561bbSGreg Roach * @param string $place_name 4784caa210SGreg Roach * @param Tree $tree 48a25f0a04SGreg Roach */ 49392561bbSGreg Roach public function __construct(string $place_name, Tree $tree) 50c1010edaSGreg Roach { 51392561bbSGreg Roach // Ignore any empty parts in place names such as "Village, , , Country". 52321a89daSGreg Roach $this->parts = Collection::make(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name)) 53392561bbSGreg Roach ->filter(); 54392561bbSGreg Roach 55392561bbSGreg Roach // Rebuild the placename in the correct format. 56392561bbSGreg Roach $this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 57392561bbSGreg Roach 5864b16745SGreg Roach $this->tree = $tree; 59a25f0a04SGreg Roach } 60a25f0a04SGreg Roach 61a25f0a04SGreg Roach /** 6276692c8bSGreg Roach * Get the higher level place. 6376692c8bSGreg Roach * 64a25f0a04SGreg Roach * @return Place 65a25f0a04SGreg Roach */ 66392561bbSGreg Roach public function parent(): Place 67c1010edaSGreg Roach { 688af6bbf8SGreg Roach return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree); 69392561bbSGreg Roach } 70392561bbSGreg Roach 71392561bbSGreg Roach /** 72392561bbSGreg Roach * The database row that contains this place. 73392561bbSGreg Roach * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 74392561bbSGreg Roach * 75392561bbSGreg Roach * @return int 76392561bbSGreg Roach */ 77392561bbSGreg Roach public function id(): int 78392561bbSGreg Roach { 7925d7fe95SGreg Roach return app('cache.array')->rememberForever(__CLASS__ . __METHOD__ . $this->place_name, function (): int { 80392561bbSGreg Roach // The "top-level" place won't exist in the database. 81392561bbSGreg Roach if ($this->parts->isEmpty()) { 82392561bbSGreg Roach return 0; 83392561bbSGreg Roach } 84392561bbSGreg Roach 858af6bbf8SGreg Roach $parent_place_id = $this->parent()->id(); 86392561bbSGreg Roach 87392561bbSGreg Roach $place_id = (int) DB::table('places') 88392561bbSGreg Roach ->where('p_file', '=', $this->tree->id()) 89392561bbSGreg Roach ->where('p_place', '=', $this->parts->first()) 908af6bbf8SGreg Roach ->where('p_parent_id', '=', $parent_place_id) 91392561bbSGreg Roach ->value('p_id'); 92392561bbSGreg Roach 93392561bbSGreg Roach if ($place_id === 0) { 94392561bbSGreg Roach $place = $this->parts->first(); 95392561bbSGreg Roach 96392561bbSGreg Roach DB::table('places')->insert([ 97392561bbSGreg Roach 'p_file' => $this->tree->id(), 98392561bbSGreg Roach 'p_place' => $place, 998af6bbf8SGreg Roach 'p_parent_id' => $parent_place_id, 100392561bbSGreg Roach 'p_std_soundex' => Soundex::russell($place), 101392561bbSGreg Roach 'p_dm_soundex' => Soundex::daitchMokotoff($place), 102392561bbSGreg Roach ]); 103392561bbSGreg Roach 104392561bbSGreg Roach $place_id = (int) DB::connection()->getPdo()->lastInsertId(); 105392561bbSGreg Roach } 106392561bbSGreg Roach 107392561bbSGreg Roach return $place_id; 108392561bbSGreg Roach }); 109392561bbSGreg Roach } 110392561bbSGreg Roach 111392561bbSGreg Roach /** 112392561bbSGreg Roach * Extract the locality (first parts) of a place name. 113392561bbSGreg Roach * 114392561bbSGreg Roach * @param int $n 115392561bbSGreg Roach * 116392561bbSGreg Roach * @return Collection 117392561bbSGreg Roach */ 118392561bbSGreg Roach public function firstParts(int $n): Collection 119392561bbSGreg Roach { 120392561bbSGreg Roach return $this->parts->slice(0, $n); 121392561bbSGreg Roach } 122392561bbSGreg Roach 123392561bbSGreg Roach /** 124392561bbSGreg Roach * Extract the country (last parts) of a place name. 125392561bbSGreg Roach * 126392561bbSGreg Roach * @param int $n 127392561bbSGreg Roach * 128392561bbSGreg Roach * @return Collection 129392561bbSGreg Roach */ 130392561bbSGreg Roach public function lastParts(int $n): Collection 131392561bbSGreg Roach { 132392561bbSGreg Roach return $this->parts->slice(-$n); 133a25f0a04SGreg Roach } 134a25f0a04SGreg Roach 135a25f0a04SGreg Roach /** 13676692c8bSGreg Roach * Get the lower level places. 13776692c8bSGreg Roach * 138a25f0a04SGreg Roach * @return Place[] 139a25f0a04SGreg Roach */ 1408f53f488SRico Sonntag public function getChildPlaces(): array 141c1010edaSGreg Roach { 142392561bbSGreg Roach if ($this->place_name !== '') { 143392561bbSGreg Roach $parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name; 144a25f0a04SGreg Roach } else { 145a25f0a04SGreg Roach $parent_text = ''; 146a25f0a04SGreg Roach } 147a25f0a04SGreg Roach 148b68caec6SGreg Roach return DB::table('places') 149b68caec6SGreg Roach ->where('p_file', '=', $this->tree->id()) 150392561bbSGreg Roach ->where('p_parent_id', '=', $this->id()) 151a69f5655SGreg Roach ->orderBy(new Expression('p_place /*! COLLATE ' . I18N::collation() . ' */')) 152b68caec6SGreg Roach ->pluck('p_place') 153b68caec6SGreg Roach ->map(function (string $place) use ($parent_text): Place { 154b68caec6SGreg Roach return new self($place . $parent_text, $this->tree); 155b68caec6SGreg Roach }) 156b68caec6SGreg Roach ->all(); 157a25f0a04SGreg Roach } 158a25f0a04SGreg Roach 159a25f0a04SGreg Roach /** 16076692c8bSGreg Roach * Create a URL to the place-hierarchy page. 16176692c8bSGreg Roach * 162a25f0a04SGreg Roach * @return string 163a25f0a04SGreg Roach */ 164e8777cb5SGreg Roach public function url(): string 165c1010edaSGreg Roach { 16667992b6aSRichard Cissee //find a module providing the place hierarchy 1670797053bSGreg Roach $module = app(ModuleService::class) 1680797053bSGreg Roach ->findByComponent(ModuleListInterface::class, $this->tree, Auth::user()) 1690797053bSGreg Roach ->first(static function (ModuleInterface $module): bool { 17067992b6aSRichard Cissee return $module instanceof PlaceHierarchyListModule; 17167992b6aSRichard Cissee }); 17267992b6aSRichard Cissee 17367992b6aSRichard Cissee if ($module instanceof PlaceHierarchyListModule) { 17467992b6aSRichard Cissee return $module->listUrl($this->tree, [ 175392561bbSGreg Roach 'parent' => $this->parts->reverse()->all(), 176*9022ab66SGreg Roach 'tree' => $this->tree->name(), 177e8777cb5SGreg Roach ]); 178e364afe4SGreg Roach } 179e364afe4SGreg Roach 180f7721877SGreg Roach // The place-list module is disabled... 181f7721877SGreg Roach return '#'; 18267992b6aSRichard Cissee } 183a25f0a04SGreg Roach 184a25f0a04SGreg Roach /** 185392561bbSGreg Roach * Format this place for GEDCOM data. 18676692c8bSGreg Roach * 187a25f0a04SGreg Roach * @return string 188a25f0a04SGreg Roach */ 189392561bbSGreg Roach public function gedcomName(): string 190c1010edaSGreg Roach { 191392561bbSGreg Roach return $this->place_name; 192a25f0a04SGreg Roach } 193a25f0a04SGreg Roach 194a25f0a04SGreg Roach /** 19576692c8bSGreg Roach * Format this place for display on screen. 19676692c8bSGreg Roach * 197a25f0a04SGreg Roach * @return string 198a25f0a04SGreg Roach */ 199392561bbSGreg Roach public function placeName(): string 200c1010edaSGreg Roach { 201392561bbSGreg Roach $place_name = $this->parts->first() ?? I18N::translate('unknown'); 202a25f0a04SGreg Roach 203392561bbSGreg Roach return '<span dir="auto">' . e($place_name) . '</span>'; 204a25f0a04SGreg Roach } 205a25f0a04SGreg Roach 206a25f0a04SGreg Roach /** 20776692c8bSGreg Roach * Generate the place name for display, including the full hierarchy. 20876692c8bSGreg Roach * 209392561bbSGreg Roach * @param bool $link 210392561bbSGreg Roach * 211a25f0a04SGreg Roach * @return string 212a25f0a04SGreg Roach */ 213e364afe4SGreg Roach public function fullName(bool $link = false): string 214c1010edaSGreg Roach { 215392561bbSGreg Roach if ($this->parts->isEmpty()) { 216392561bbSGreg Roach return ''; 217b2ce94c6SRico Sonntag } 218b2ce94c6SRico Sonntag 219392561bbSGreg Roach $full_name = $this->parts->implode(I18N::$list_separator); 220392561bbSGreg Roach 221392561bbSGreg Roach if ($link) { 222392561bbSGreg Roach return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>'; 223a25f0a04SGreg Roach } 224a25f0a04SGreg Roach 225392561bbSGreg Roach return '<span dir="auto">' . e($full_name) . '</span>'; 226a25f0a04SGreg Roach } 227a25f0a04SGreg Roach 228a25f0a04SGreg Roach /** 229a25f0a04SGreg Roach * For lists and charts, where the full name won’t fit. 230a25f0a04SGreg Roach * 231392561bbSGreg Roach * @param bool $link 232a25f0a04SGreg Roach * 233a25f0a04SGreg Roach * @return string 234a25f0a04SGreg Roach */ 235e364afe4SGreg Roach public function shortName(bool $link = false): string 236c1010edaSGreg Roach { 237392561bbSGreg Roach $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); 238392561bbSGreg Roach 239392561bbSGreg Roach // Abbreviate the place name, for lists 240392561bbSGreg Roach if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { 241392561bbSGreg Roach $parts = $this->lastParts($SHOW_PEDIGREE_PLACES); 242392561bbSGreg Roach } else { 243392561bbSGreg Roach $parts = $this->firstParts($SHOW_PEDIGREE_PLACES); 244a25f0a04SGreg Roach } 245a25f0a04SGreg Roach 246392561bbSGreg Roach $short_name = $parts->implode(I18N::$list_separator); 247392561bbSGreg Roach 248392561bbSGreg Roach // Add a tool-tip showing the full name 249392561bbSGreg Roach $title = strip_tags($this->fullName()); 250392561bbSGreg Roach 251392561bbSGreg Roach if ($link) { 25240f99dd2SGreg Roach return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '">' . e($short_name) . '</a>'; 253392561bbSGreg Roach } 254392561bbSGreg Roach 255392561bbSGreg Roach return '<span dir="auto">' . e($short_name) . '</span>'; 256a25f0a04SGreg Roach } 257a25f0a04SGreg Roach} 258