1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 17a25f0a04SGreg Roach 18a25f0a04SGreg Roach/** 1976692c8bSGreg Roach * A GEDCOM place (PLAC) object. 20a25f0a04SGreg Roach */ 21c1010edaSGreg Roachclass Place 22c1010edaSGreg Roach{ 23a25f0a04SGreg Roach const GEDCOM_SEPARATOR = ', '; 2484caa210SGreg Roach 2576692c8bSGreg Roach /** @var string[] e.g. array('Westminster', 'London', 'England') */ 2684caa210SGreg Roach private $gedcom_place; 2784caa210SGreg Roach 2876692c8bSGreg Roach /** @var Tree We may have the same place name in different trees. */ 2984caa210SGreg Roach private $tree; 30a25f0a04SGreg Roach 31a25f0a04SGreg Roach /** 3276692c8bSGreg Roach * Create a place. 3376692c8bSGreg Roach * 34a25f0a04SGreg Roach * @param string $gedcom_place 3584caa210SGreg Roach * @param Tree $tree 36a25f0a04SGreg Roach */ 37c1010edaSGreg Roach public function __construct($gedcom_place, Tree $tree) 38c1010edaSGreg Roach { 39b20ddbf9SGreg Roach if ($gedcom_place === '') { 4013abd6f3SGreg Roach $this->gedcom_place = []; 41b20ddbf9SGreg Roach } else { 42b20ddbf9SGreg Roach $this->gedcom_place = explode(self::GEDCOM_SEPARATOR, $gedcom_place); 43a25f0a04SGreg Roach } 4464b16745SGreg Roach $this->tree = $tree; 45a25f0a04SGreg Roach } 46a25f0a04SGreg Roach 47a25f0a04SGreg Roach /** 4816d0b7f7SRico Sonntag * Extract the country (last part) of a place name. 4916d0b7f7SRico Sonntag * 5016d0b7f7SRico Sonntag * @return string - e.g. "England" 5116d0b7f7SRico Sonntag */ 52c1010edaSGreg Roach public function lastPart() 53c1010edaSGreg Roach { 5416d0b7f7SRico Sonntag return end($this->gedcom_place); 5516d0b7f7SRico Sonntag } 5616d0b7f7SRico Sonntag 5716d0b7f7SRico Sonntag /** 5876692c8bSGreg Roach * Get the identifier for a place. 5976692c8bSGreg Roach * 60cbc1590aSGreg Roach * @return int 61661f69c0SDavid Drury * @throws \Exception 62a25f0a04SGreg Roach */ 63c1010edaSGreg Roach public function getPlaceId() 64c1010edaSGreg Roach { 65a25f0a04SGreg Roach $place_id = 0; 66a25f0a04SGreg Roach foreach (array_reverse($this->gedcom_place) as $place) { 67a25f0a04SGreg Roach $place_id = Database::prepare( 68e5588fb0SGreg Roach "SELECT p_id FROM `##places` WHERE p_parent_id = :parent_id AND p_place = :place AND p_file = :tree_id" 6913abd6f3SGreg Roach )->execute([ 70a25f0a04SGreg Roach 'parent_id' => $place_id, 71a25f0a04SGreg Roach 'place' => $place, 7264b16745SGreg Roach 'tree_id' => $this->tree->getTreeId(), 7313abd6f3SGreg Roach ])->fetchOne(); 74a25f0a04SGreg Roach } 75a25f0a04SGreg Roach 76a25f0a04SGreg Roach return $place_id; 77a25f0a04SGreg Roach } 78a25f0a04SGreg Roach 79a25f0a04SGreg Roach /** 8076692c8bSGreg Roach * Get the higher level place. 8176692c8bSGreg Roach * 82a25f0a04SGreg Roach * @return Place 83a25f0a04SGreg Roach */ 84c1010edaSGreg Roach public function getParentPlace() 85c1010edaSGreg Roach { 8606ef8e02SGreg Roach return new self(implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 1)), $this->tree); 87a25f0a04SGreg Roach } 88a25f0a04SGreg Roach 89a25f0a04SGreg Roach /** 9076692c8bSGreg Roach * Get the lower level places. 9176692c8bSGreg Roach * 92a25f0a04SGreg Roach * @return Place[] 93661f69c0SDavid Drury * @throws \Exception 94a25f0a04SGreg Roach */ 95c1010edaSGreg Roach public function getChildPlaces() 96c1010edaSGreg Roach { 9713abd6f3SGreg Roach $children = []; 98a25f0a04SGreg Roach if ($this->getPlaceId()) { 99a25f0a04SGreg Roach $parent_text = self::GEDCOM_SEPARATOR . $this->getGedcomName(); 100a25f0a04SGreg Roach } else { 101a25f0a04SGreg Roach $parent_text = ''; 102a25f0a04SGreg Roach } 103a25f0a04SGreg Roach 104a25f0a04SGreg Roach $rows = Database::prepare( 105e5588fb0SGreg Roach "SELECT p_place FROM `##places`" . 106a25f0a04SGreg Roach " WHERE p_parent_id = :parent_id AND p_file = :tree_id" . 107a25f0a04SGreg Roach " ORDER BY p_place COLLATE :collation" 10813abd6f3SGreg Roach )->execute([ 109a25f0a04SGreg Roach 'parent_id' => $this->getPlaceId(), 11064b16745SGreg Roach 'tree_id' => $this->tree->getTreeId(), 11181088023SGreg Roach 'collation' => I18N::collation(), 11213abd6f3SGreg Roach ])->fetchOneColumn(); 113a25f0a04SGreg Roach foreach ($rows as $row) { 11406ef8e02SGreg Roach $children[] = new self($row . $parent_text, $this->tree); 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach 117a25f0a04SGreg Roach return $children; 118a25f0a04SGreg Roach } 119a25f0a04SGreg Roach 120a25f0a04SGreg Roach /** 12176692c8bSGreg Roach * Create a URL to the place-hierarchy page. 12276692c8bSGreg Roach * 123a25f0a04SGreg Roach * @return string 124a25f0a04SGreg Roach */ 125c1010edaSGreg Roach public function getURL() 126c1010edaSGreg Roach { 127*4686330aSGreg Roach return e(route('place-hierarchy', [ 128c1010edaSGreg Roach 'parent' => array_reverse($this->gedcom_place), 129c1010edaSGreg Roach 'ged' => $this->tree->getName(), 130c1010edaSGreg Roach ])); 131a25f0a04SGreg Roach } 132a25f0a04SGreg Roach 133a25f0a04SGreg Roach /** 13476692c8bSGreg Roach * Format this name for GEDCOM data. 13576692c8bSGreg Roach * 136a25f0a04SGreg Roach * @return string 137a25f0a04SGreg Roach */ 138c1010edaSGreg Roach public function getGedcomName() 139c1010edaSGreg Roach { 140a25f0a04SGreg Roach return implode(self::GEDCOM_SEPARATOR, $this->gedcom_place); 141a25f0a04SGreg Roach } 142a25f0a04SGreg Roach 143a25f0a04SGreg Roach /** 14476692c8bSGreg Roach * Format this place for display on screen. 14576692c8bSGreg Roach * 146a25f0a04SGreg Roach * @return string 147a25f0a04SGreg Roach */ 148c1010edaSGreg Roach public function getPlaceName() 149c1010edaSGreg Roach { 150a25f0a04SGreg Roach $place = reset($this->gedcom_place); 151a25f0a04SGreg Roach 152d53324c9SGreg Roach return $place ? '<span dir="auto">' . e($place) . '</span>' : I18N::translate('unknown'); 153a25f0a04SGreg Roach } 154a25f0a04SGreg Roach 155a25f0a04SGreg Roach /** 15676692c8bSGreg Roach * Is this a null/empty/missing/invalid place? 15776692c8bSGreg Roach * 158a25f0a04SGreg Roach * @return bool 159a25f0a04SGreg Roach */ 160c1010edaSGreg Roach public function isEmpty() 161c1010edaSGreg Roach { 162a25f0a04SGreg Roach return empty($this->gedcom_place); 163a25f0a04SGreg Roach } 164a25f0a04SGreg Roach 165a25f0a04SGreg Roach /** 16676692c8bSGreg Roach * Generate the place name for display, including the full hierarchy. 16776692c8bSGreg Roach * 168a25f0a04SGreg Roach * @return string 169a25f0a04SGreg Roach */ 170c1010edaSGreg Roach public function getFullName() 171c1010edaSGreg Roach { 172a25f0a04SGreg Roach if (true) { 173a25f0a04SGreg Roach // If a place hierarchy is a single entity 174d53324c9SGreg Roach return '<span dir="auto">' . e(implode(I18N::$list_separator, $this->gedcom_place)) . '</span>'; 175a25f0a04SGreg Roach } else { 176a25f0a04SGreg Roach // If a place hierarchy is a list of distinct items 17713abd6f3SGreg Roach $tmp = []; 178a25f0a04SGreg Roach foreach ($this->gedcom_place as $place) { 179d53324c9SGreg Roach $tmp[] = '<span dir="auto">' . e($place) . '</span>'; 180a25f0a04SGreg Roach } 181a25f0a04SGreg Roach 182a25f0a04SGreg Roach return implode(I18N::$list_separator, $tmp); 183a25f0a04SGreg Roach } 184a25f0a04SGreg Roach } 185a25f0a04SGreg Roach 186a25f0a04SGreg Roach /** 187a25f0a04SGreg Roach * For lists and charts, where the full name won’t fit. 188a25f0a04SGreg Roach * 189a25f0a04SGreg Roach * @return string 190a25f0a04SGreg Roach */ 191c1010edaSGreg Roach public function getShortName() 192c1010edaSGreg Roach { 193a99c6938SGreg Roach $SHOW_PEDIGREE_PLACES = (int)$this->tree->getPreference('SHOW_PEDIGREE_PLACES'); 194a25f0a04SGreg Roach 195a25f0a04SGreg Roach if ($SHOW_PEDIGREE_PLACES >= count($this->gedcom_place)) { 196a25f0a04SGreg Roach // A short place name - no need to abbreviate 197a25f0a04SGreg Roach return $this->getFullName(); 198a25f0a04SGreg Roach } else { 199a25f0a04SGreg Roach // Abbreviate the place name, for lists 20064b16745SGreg Roach if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { 201a25f0a04SGreg Roach // The *last* $SHOW_PEDIGREE_PLACES components 202a25f0a04SGreg Roach $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, -$SHOW_PEDIGREE_PLACES)); 203a25f0a04SGreg Roach } else { 204a25f0a04SGreg Roach // The *first* $SHOW_PEDIGREE_PLACES components 205a25f0a04SGreg Roach $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 0, $SHOW_PEDIGREE_PLACES)); 206a25f0a04SGreg Roach } 207c1010edaSGreg Roach 208a25f0a04SGreg Roach // Add a tool-tip showing the full name 209d53324c9SGreg Roach return '<span title="' . e($this->getGedcomName()) . '" dir="auto">' . e($short_name) . '</span>'; 210a25f0a04SGreg Roach } 211a25f0a04SGreg Roach } 212a25f0a04SGreg Roach 213a25f0a04SGreg Roach /** 214661f69c0SDavid Drury * For the Place hierarchy "list all" option 215a25f0a04SGreg Roach * 216a25f0a04SGreg Roach * @return string 217a25f0a04SGreg Roach */ 218c1010edaSGreg Roach public function getReverseName() 219c1010edaSGreg Roach { 22013abd6f3SGreg Roach $tmp = []; 221a25f0a04SGreg Roach foreach (array_reverse($this->gedcom_place) as $place) { 222d53324c9SGreg Roach $tmp[] = '<span dir="auto">' . e($place) . '</span>'; 223a25f0a04SGreg Roach } 224a25f0a04SGreg Roach 225a25f0a04SGreg Roach return implode(I18N::$list_separator, $tmp); 226a25f0a04SGreg Roach } 227a25f0a04SGreg Roach 228a25f0a04SGreg Roach /** 22976692c8bSGreg Roach * Fetch all places from the database. 23076692c8bSGreg Roach * 23184caa210SGreg Roach * @param Tree $tree 232c1010edaSGreg Roach * 2334080d558SGreg Roach * @return Place[] 234661f69c0SDavid Drury * @throws \Exception 235a25f0a04SGreg Roach */ 236c1010edaSGreg Roach public static function allPlaces(Tree $tree) 237c1010edaSGreg Roach { 23813abd6f3SGreg Roach $places = []; 239a25f0a04SGreg Roach $rows = 240a25f0a04SGreg Roach Database::prepare( 241e5588fb0SGreg 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)" . 242a25f0a04SGreg Roach " FROM `##places` AS p1" . 243a25f0a04SGreg Roach " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" . 244a25f0a04SGreg Roach " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" . 245a25f0a04SGreg Roach " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" . 246a25f0a04SGreg Roach " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" . 247a25f0a04SGreg Roach " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" . 248a25f0a04SGreg Roach " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" . 249a25f0a04SGreg Roach " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" . 250a25f0a04SGreg Roach " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" . 25184caa210SGreg Roach " WHERE p1.p_file = :tree_id" . 252288566a3SGreg 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" 253a25f0a04SGreg Roach ) 25413abd6f3SGreg Roach ->execute([ 255288566a3SGreg Roach 'tree_id' => $tree->getTreeId(), 25681088023SGreg Roach 'collate' => I18N::collation(), 25713abd6f3SGreg Roach ])->fetchOneColumn(); 258a25f0a04SGreg Roach foreach ($rows as $row) { 25906ef8e02SGreg Roach $places[] = new self($row, $tree); 260a25f0a04SGreg Roach } 261cbc1590aSGreg Roach 262a25f0a04SGreg Roach return $places; 263a25f0a04SGreg Roach } 264a25f0a04SGreg Roach 265a25f0a04SGreg Roach /** 26676692c8bSGreg Roach * Search for a place name. 26776692c8bSGreg Roach * 268a25f0a04SGreg Roach * @param string $filter 26984caa210SGreg Roach * @param Tree $tree 270c1010edaSGreg Roach * 2714080d558SGreg Roach * @return Place[] 272661f69c0SDavid Drury * @throws \Exception 273a25f0a04SGreg Roach */ 274c1010edaSGreg Roach public static function findPlaces($filter, Tree $tree) 275c1010edaSGreg Roach { 27613abd6f3SGreg Roach $places = []; 277a25f0a04SGreg Roach $rows = 278a25f0a04SGreg Roach Database::prepare( 279e5588fb0SGreg 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)" . 280a25f0a04SGreg Roach " FROM `##places` AS p1" . 281a25f0a04SGreg Roach " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" . 282a25f0a04SGreg Roach " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" . 283a25f0a04SGreg Roach " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" . 284a25f0a04SGreg Roach " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" . 285a25f0a04SGreg Roach " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" . 286a25f0a04SGreg Roach " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" . 287a25f0a04SGreg Roach " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" . 288a25f0a04SGreg Roach " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" . 28984caa210SGreg 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" . 29081088023SGreg 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" 29113abd6f3SGreg Roach )->execute([ 29284caa210SGreg Roach 'filter_1' => preg_quote($filter), 29384caa210SGreg Roach 'filter_2' => preg_quote($filter), 29481088023SGreg Roach 'tree_id' => $tree->getTreeId(), 29581088023SGreg Roach 'collation' => I18N::collation(), 29613abd6f3SGreg Roach ])->fetchOneColumn(); 297a25f0a04SGreg Roach foreach ($rows as $row) { 29806ef8e02SGreg Roach $places[] = new self($row, $tree); 299a25f0a04SGreg Roach } 300cbc1590aSGreg Roach 301a25f0a04SGreg Roach return $places; 302a25f0a04SGreg Roach } 303a25f0a04SGreg Roach} 304