1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 19a25f0a04SGreg Roach 20a25f0a04SGreg Roach/** 2176692c8bSGreg Roach * A GEDCOM place (PLAC) object. 22a25f0a04SGreg Roach */ 23c1010edaSGreg Roachclass Place 24c1010edaSGreg Roach{ 25a25f0a04SGreg Roach const GEDCOM_SEPARATOR = ', '; 2684caa210SGreg Roach 2776692c8bSGreg Roach /** @var string[] e.g. array('Westminster', 'London', 'England') */ 2884caa210SGreg Roach private $gedcom_place; 2984caa210SGreg Roach 3076692c8bSGreg Roach /** @var Tree We may have the same place name in different trees. */ 3184caa210SGreg Roach private $tree; 32a25f0a04SGreg Roach 33a25f0a04SGreg Roach /** 3476692c8bSGreg Roach * Create a place. 3576692c8bSGreg Roach * 36a25f0a04SGreg Roach * @param string $gedcom_place 3784caa210SGreg Roach * @param Tree $tree 38a25f0a04SGreg Roach */ 39c1010edaSGreg Roach public function __construct($gedcom_place, Tree $tree) 40c1010edaSGreg Roach { 41b20ddbf9SGreg Roach if ($gedcom_place === '') { 4213abd6f3SGreg Roach $this->gedcom_place = []; 43b20ddbf9SGreg Roach } else { 44b20ddbf9SGreg Roach $this->gedcom_place = explode(self::GEDCOM_SEPARATOR, $gedcom_place); 45a25f0a04SGreg Roach } 4664b16745SGreg Roach $this->tree = $tree; 47a25f0a04SGreg Roach } 48a25f0a04SGreg Roach 49a25f0a04SGreg Roach /** 5016d0b7f7SRico Sonntag * Extract the country (last part) of a place name. 5116d0b7f7SRico Sonntag * 5216d0b7f7SRico Sonntag * @return string - e.g. "England" 5316d0b7f7SRico Sonntag */ 548f53f488SRico Sonntag public function lastPart(): string 55c1010edaSGreg Roach { 5675fe6325SGreg Roach return $this->gedcom_place[count($this->gedcom_place) - 1] ?? ''; 5716d0b7f7SRico Sonntag } 5816d0b7f7SRico Sonntag 5916d0b7f7SRico Sonntag /** 6076692c8bSGreg Roach * Get the identifier for a place. 6176692c8bSGreg Roach * 62cbc1590aSGreg Roach * @return int 63a25f0a04SGreg Roach */ 648f53f488SRico Sonntag public function getPlaceId(): int 65c1010edaSGreg Roach { 66a25f0a04SGreg Roach $place_id = 0; 67db7bb364SGreg Roach 68a25f0a04SGreg Roach foreach (array_reverse($this->gedcom_place) as $place) { 69db7bb364SGreg Roach $place_id = (int) Database::prepare( 70e5588fb0SGreg Roach "SELECT p_id FROM `##places` WHERE p_parent_id = :parent_id AND p_place = :place AND p_file = :tree_id" 7113abd6f3SGreg Roach )->execute([ 72a25f0a04SGreg Roach 'parent_id' => $place_id, 73a25f0a04SGreg Roach 'place' => $place, 7472cf66d4SGreg Roach 'tree_id' => $this->tree->id(), 7513abd6f3SGreg Roach ])->fetchOne(); 76a25f0a04SGreg Roach } 77a25f0a04SGreg Roach 78a25f0a04SGreg Roach return $place_id; 79a25f0a04SGreg Roach } 80a25f0a04SGreg Roach 81a25f0a04SGreg Roach /** 8276692c8bSGreg Roach * Get the higher level place. 8376692c8bSGreg Roach * 84a25f0a04SGreg Roach * @return Place 85a25f0a04SGreg Roach */ 868f53f488SRico Sonntag public function getParentPlace(): Place 87c1010edaSGreg Roach { 8806ef8e02SGreg Roach return new self(implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 1)), $this->tree); 89a25f0a04SGreg Roach } 90a25f0a04SGreg Roach 91a25f0a04SGreg Roach /** 9276692c8bSGreg Roach * Get the lower level places. 9376692c8bSGreg Roach * 94a25f0a04SGreg Roach * @return Place[] 95a25f0a04SGreg Roach */ 968f53f488SRico Sonntag public function getChildPlaces(): array 97c1010edaSGreg Roach { 9813abd6f3SGreg Roach $children = []; 99a25f0a04SGreg Roach if ($this->getPlaceId()) { 100a25f0a04SGreg Roach $parent_text = self::GEDCOM_SEPARATOR . $this->getGedcomName(); 101a25f0a04SGreg Roach } else { 102a25f0a04SGreg Roach $parent_text = ''; 103a25f0a04SGreg Roach } 104a25f0a04SGreg Roach 105a25f0a04SGreg Roach $rows = Database::prepare( 106e5588fb0SGreg Roach "SELECT p_place FROM `##places`" . 107a25f0a04SGreg Roach " WHERE p_parent_id = :parent_id AND p_file = :tree_id" . 108a25f0a04SGreg Roach " ORDER BY p_place COLLATE :collation" 10913abd6f3SGreg Roach )->execute([ 110a25f0a04SGreg Roach 'parent_id' => $this->getPlaceId(), 11172cf66d4SGreg Roach 'tree_id' => $this->tree->id(), 11281088023SGreg Roach 'collation' => I18N::collation(), 11313abd6f3SGreg Roach ])->fetchOneColumn(); 114a25f0a04SGreg Roach foreach ($rows as $row) { 11506ef8e02SGreg Roach $children[] = new self($row . $parent_text, $this->tree); 116a25f0a04SGreg Roach } 117a25f0a04SGreg Roach 118a25f0a04SGreg Roach return $children; 119a25f0a04SGreg Roach } 120a25f0a04SGreg Roach 121a25f0a04SGreg Roach /** 12276692c8bSGreg Roach * Create a URL to the place-hierarchy page. 12376692c8bSGreg Roach * 124a25f0a04SGreg Roach * @return string 125a25f0a04SGreg Roach */ 126e8777cb5SGreg Roach public function url(): string 127c1010edaSGreg Roach { 128e8777cb5SGreg Roach return route('place-hierarchy', [ 129c1010edaSGreg Roach 'parent' => array_reverse($this->gedcom_place), 130aa6f03bbSGreg Roach 'ged' => $this->tree->name(), 131e8777cb5SGreg Roach ]); 132a25f0a04SGreg Roach } 133a25f0a04SGreg Roach 134a25f0a04SGreg Roach /** 13576692c8bSGreg Roach * Format this name for GEDCOM data. 13676692c8bSGreg Roach * 137a25f0a04SGreg Roach * @return string 138a25f0a04SGreg Roach */ 1398f53f488SRico Sonntag public function getGedcomName(): string 140c1010edaSGreg Roach { 141a25f0a04SGreg Roach return implode(self::GEDCOM_SEPARATOR, $this->gedcom_place); 142a25f0a04SGreg Roach } 143a25f0a04SGreg Roach 144a25f0a04SGreg Roach /** 14576692c8bSGreg Roach * Format this place for display on screen. 14676692c8bSGreg Roach * 147a25f0a04SGreg Roach * @return string 148a25f0a04SGreg Roach */ 1498f53f488SRico Sonntag public function getPlaceName(): string 150c1010edaSGreg Roach { 15175fe6325SGreg Roach if (empty($this->gedcom_place)) { 15275fe6325SGreg Roach return I18N::translate('unknown'); 15375fe6325SGreg Roach } 154a25f0a04SGreg Roach 15575fe6325SGreg Roach return '<span dir="auto">' . e($this->gedcom_place[0]) . '</span>'; 156a25f0a04SGreg Roach } 157a25f0a04SGreg Roach 158a25f0a04SGreg Roach /** 15976692c8bSGreg Roach * Is this a null/empty/missing/invalid place? 16076692c8bSGreg Roach * 161a25f0a04SGreg Roach * @return bool 162a25f0a04SGreg Roach */ 1638f53f488SRico Sonntag public function isEmpty(): bool 164c1010edaSGreg Roach { 165a25f0a04SGreg Roach return empty($this->gedcom_place); 166a25f0a04SGreg Roach } 167a25f0a04SGreg Roach 168a25f0a04SGreg Roach /** 16976692c8bSGreg Roach * Generate the place name for display, including the full hierarchy. 17076692c8bSGreg Roach * 171a25f0a04SGreg Roach * @return string 172a25f0a04SGreg Roach */ 173c1010edaSGreg Roach public function getFullName() 174c1010edaSGreg Roach { 175a25f0a04SGreg Roach if (true) { 176a25f0a04SGreg Roach // If a place hierarchy is a single entity 177d53324c9SGreg Roach return '<span dir="auto">' . e(implode(I18N::$list_separator, $this->gedcom_place)) . '</span>'; 178b2ce94c6SRico Sonntag } 179b2ce94c6SRico Sonntag 180a25f0a04SGreg Roach // If a place hierarchy is a list of distinct items 18113abd6f3SGreg Roach $tmp = []; 182a25f0a04SGreg Roach foreach ($this->gedcom_place as $place) { 183d53324c9SGreg Roach $tmp[] = '<span dir="auto">' . e($place) . '</span>'; 184a25f0a04SGreg Roach } 185a25f0a04SGreg Roach 186a25f0a04SGreg Roach return implode(I18N::$list_separator, $tmp); 187a25f0a04SGreg Roach } 188a25f0a04SGreg Roach 189a25f0a04SGreg Roach /** 190a25f0a04SGreg Roach * For lists and charts, where the full name won’t fit. 191a25f0a04SGreg Roach * 192a25f0a04SGreg Roach * @return string 193a25f0a04SGreg Roach */ 194c1010edaSGreg Roach public function getShortName() 195c1010edaSGreg Roach { 196a99c6938SGreg Roach $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); 197a25f0a04SGreg Roach 198a25f0a04SGreg Roach if ($SHOW_PEDIGREE_PLACES >= count($this->gedcom_place)) { 199a25f0a04SGreg Roach // A short place name - no need to abbreviate 200a25f0a04SGreg Roach return $this->getFullName(); 201b2ce94c6SRico Sonntag } 202b2ce94c6SRico Sonntag 203a25f0a04SGreg Roach // Abbreviate the place name, for lists 20464b16745SGreg Roach if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { 205a25f0a04SGreg Roach // The *last* $SHOW_PEDIGREE_PLACES components 206a25f0a04SGreg Roach $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, -$SHOW_PEDIGREE_PLACES)); 207a25f0a04SGreg Roach } else { 208a25f0a04SGreg Roach // The *first* $SHOW_PEDIGREE_PLACES components 209a25f0a04SGreg Roach $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 0, $SHOW_PEDIGREE_PLACES)); 210a25f0a04SGreg Roach } 211c1010edaSGreg Roach 212a25f0a04SGreg Roach // Add a tool-tip showing the full name 213d53324c9SGreg Roach return '<span title="' . e($this->getGedcomName()) . '" dir="auto">' . e($short_name) . '</span>'; 214a25f0a04SGreg Roach } 215a25f0a04SGreg Roach 216a25f0a04SGreg Roach /** 217661f69c0SDavid Drury * For the Place hierarchy "list all" option 218a25f0a04SGreg Roach * 219a25f0a04SGreg Roach * @return string 220a25f0a04SGreg Roach */ 2218f53f488SRico Sonntag public function getReverseName(): string 222c1010edaSGreg Roach { 22313abd6f3SGreg Roach $tmp = []; 224a25f0a04SGreg Roach foreach (array_reverse($this->gedcom_place) as $place) { 225d53324c9SGreg Roach $tmp[] = '<span dir="auto">' . e($place) . '</span>'; 226a25f0a04SGreg Roach } 227a25f0a04SGreg Roach 228a25f0a04SGreg Roach return implode(I18N::$list_separator, $tmp); 229a25f0a04SGreg Roach } 230a25f0a04SGreg Roach 231a25f0a04SGreg Roach /** 23276692c8bSGreg Roach * Fetch all places from the database. 23376692c8bSGreg Roach * 23484caa210SGreg Roach * @param Tree $tree 235c1010edaSGreg Roach * 2364080d558SGreg Roach * @return Place[] 237a25f0a04SGreg Roach */ 2388f53f488SRico Sonntag public static function allPlaces(Tree $tree): array 239c1010edaSGreg Roach { 24013abd6f3SGreg Roach $places = []; 241a25f0a04SGreg Roach $rows = 242a25f0a04SGreg Roach Database::prepare( 243e5588fb0SGreg Roach "SELECT CONCAT_WS(', ', p1.p_place, p2.p_place, p3.p_place, p4.p_place, p5.p_place, p6.p_place, p7.p_place, p8.p_place, p9.p_place)" . 244a25f0a04SGreg Roach " FROM `##places` AS p1" . 245a25f0a04SGreg Roach " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" . 246a25f0a04SGreg Roach " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" . 247a25f0a04SGreg Roach " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" . 248a25f0a04SGreg Roach " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" . 249a25f0a04SGreg Roach " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" . 250a25f0a04SGreg Roach " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" . 251a25f0a04SGreg Roach " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" . 252a25f0a04SGreg Roach " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" . 25384caa210SGreg Roach " WHERE p1.p_file = :tree_id" . 254288566a3SGreg Roach " ORDER BY CONCAT_WS(', ', p9.p_place, p8.p_place, p7.p_place, p6.p_place, p5.p_place, p4.p_place, p3.p_place, p2.p_place, p1.p_place) COLLATE :collate" 255a25f0a04SGreg Roach ) 25613abd6f3SGreg Roach ->execute([ 25772cf66d4SGreg Roach 'tree_id' => $tree->id(), 25881088023SGreg Roach 'collate' => I18N::collation(), 25913abd6f3SGreg Roach ])->fetchOneColumn(); 260a25f0a04SGreg Roach foreach ($rows as $row) { 26106ef8e02SGreg Roach $places[] = new self($row, $tree); 262a25f0a04SGreg Roach } 263cbc1590aSGreg Roach 264a25f0a04SGreg Roach return $places; 265a25f0a04SGreg Roach } 266a25f0a04SGreg Roach 267a25f0a04SGreg Roach /** 26876692c8bSGreg Roach * Search for a place name. 26976692c8bSGreg Roach * 270a25f0a04SGreg Roach * @param string $filter 27184caa210SGreg Roach * @param Tree $tree 272c1010edaSGreg Roach * 2734080d558SGreg Roach * @return Place[] 274a25f0a04SGreg Roach */ 2758f53f488SRico Sonntag public static function findPlaces($filter, Tree $tree): array 276c1010edaSGreg Roach { 27713abd6f3SGreg Roach $places = []; 278a25f0a04SGreg Roach $rows = 279a25f0a04SGreg Roach Database::prepare( 280e5588fb0SGreg Roach "SELECT CONCAT_WS(', ', p1.p_place, p2.p_place, p3.p_place, p4.p_place, p5.p_place, p6.p_place, p7.p_place, p8.p_place, p9.p_place)" . 281a25f0a04SGreg Roach " FROM `##places` AS p1" . 282a25f0a04SGreg Roach " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" . 283a25f0a04SGreg Roach " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" . 284a25f0a04SGreg Roach " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" . 285a25f0a04SGreg Roach " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" . 286a25f0a04SGreg Roach " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" . 287a25f0a04SGreg Roach " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" . 288a25f0a04SGreg Roach " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" . 289a25f0a04SGreg Roach " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" . 29084caa210SGreg Roach " WHERE CONCAT_WS(', ', p1.p_place, p2.p_place, p3.p_place, p4.p_place, p5.p_place, p6.p_place, p7.p_place, p8.p_place, p9.p_place) LIKE CONCAT('%', :filter_1, '%') AND CONCAT_WS(', ', p1.p_place, p2.p_place, p3.p_place, p4.p_place, p5.p_place, p6.p_place, p7.p_place, p8.p_place, p9.p_place) NOT LIKE CONCAT('%,%', :filter_2, '%') AND p1.p_file = :tree_id" . 29181088023SGreg Roach " ORDER BY CONCAT_WS(', ', p1.p_place, p2.p_place, p3.p_place, p4.p_place, p5.p_place, p6.p_place, p7.p_place, p8.p_place, p9.p_place) COLLATE :collation" 29213abd6f3SGreg Roach )->execute([ 29384caa210SGreg Roach 'filter_1' => preg_quote($filter), 29484caa210SGreg Roach 'filter_2' => preg_quote($filter), 29572cf66d4SGreg Roach 'tree_id' => $tree->id(), 29681088023SGreg Roach 'collation' => I18N::collation(), 29713abd6f3SGreg Roach ])->fetchOneColumn(); 298a25f0a04SGreg Roach foreach ($rows as $row) { 29906ef8e02SGreg Roach $places[] = new self($row, $tree); 300a25f0a04SGreg Roach } 301cbc1590aSGreg Roach 302a25f0a04SGreg Roach return $places; 303a25f0a04SGreg Roach } 304a25f0a04SGreg Roach} 305