1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 */ 16namespace Fisharebest\Webtrees; 17 18/** 19 * A GEDCOM place (PLAC) object. 20 */ 21class Place 22{ 23 const GEDCOM_SEPARATOR = ', '; 24 25 /** @var string[] e.g. array('Westminster', 'London', 'England') */ 26 private $gedcom_place; 27 28 /** @var Tree We may have the same place name in different trees. */ 29 private $tree; 30 31 /** 32 * Create a place. 33 * 34 * @param string $gedcom_place 35 * @param Tree $tree 36 */ 37 public function __construct($gedcom_place, Tree $tree) 38 { 39 if ($gedcom_place === '') { 40 $this->gedcom_place = []; 41 } else { 42 $this->gedcom_place = explode(self::GEDCOM_SEPARATOR, $gedcom_place); 43 } 44 $this->tree = $tree; 45 } 46 47 /** 48 * Extract the country (last part) of a place name. 49 * 50 * @return string - e.g. "England" 51 */ 52 public function lastPart(): string 53 { 54 return end($this->gedcom_place); 55 } 56 57 /** 58 * Get the identifier for a place. 59 * 60 * @return int 61 */ 62 public function getPlaceId(): int 63 { 64 $place_id = 0; 65 66 foreach (array_reverse($this->gedcom_place) as $place) { 67 $place_id = (int) Database::prepare( 68 "SELECT p_id FROM `##places` WHERE p_parent_id = :parent_id AND p_place = :place AND p_file = :tree_id" 69 )->execute([ 70 'parent_id' => $place_id, 71 'place' => $place, 72 'tree_id' => $this->tree->getTreeId(), 73 ])->fetchOne(); 74 } 75 76 return $place_id; 77 } 78 79 /** 80 * Get the higher level place. 81 * 82 * @return Place 83 */ 84 public function getParentPlace(): Place 85 { 86 return new self(implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 1)), $this->tree); 87 } 88 89 /** 90 * Get the lower level places. 91 * 92 * @return Place[] 93 */ 94 public function getChildPlaces(): array 95 { 96 $children = []; 97 if ($this->getPlaceId()) { 98 $parent_text = self::GEDCOM_SEPARATOR . $this->getGedcomName(); 99 } else { 100 $parent_text = ''; 101 } 102 103 $rows = Database::prepare( 104 "SELECT p_place FROM `##places`" . 105 " WHERE p_parent_id = :parent_id AND p_file = :tree_id" . 106 " ORDER BY p_place COLLATE :collation" 107 )->execute([ 108 'parent_id' => $this->getPlaceId(), 109 'tree_id' => $this->tree->getTreeId(), 110 'collation' => I18N::collation(), 111 ])->fetchOneColumn(); 112 foreach ($rows as $row) { 113 $children[] = new self($row . $parent_text, $this->tree); 114 } 115 116 return $children; 117 } 118 119 /** 120 * Create a URL to the place-hierarchy page. 121 * 122 * @return string 123 */ 124 public function getURL(): string 125 { 126 return e(route('place-hierarchy', [ 127 'parent' => array_reverse($this->gedcom_place), 128 'ged' => $this->tree->getName(), 129 ])); 130 } 131 132 /** 133 * Format this name for GEDCOM data. 134 * 135 * @return string 136 */ 137 public function getGedcomName(): string 138 { 139 return implode(self::GEDCOM_SEPARATOR, $this->gedcom_place); 140 } 141 142 /** 143 * Format this place for display on screen. 144 * 145 * @return string 146 */ 147 public function getPlaceName(): string 148 { 149 $place = reset($this->gedcom_place); 150 151 return $place ? '<span dir="auto">' . e($place) . '</span>' : I18N::translate('unknown'); 152 } 153 154 /** 155 * Is this a null/empty/missing/invalid place? 156 * 157 * @return bool 158 */ 159 public function isEmpty(): bool 160 { 161 return empty($this->gedcom_place); 162 } 163 164 /** 165 * Generate the place name for display, including the full hierarchy. 166 * 167 * @return string 168 */ 169 public function getFullName() 170 { 171 if (true) { 172 // If a place hierarchy is a single entity 173 return '<span dir="auto">' . e(implode(I18N::$list_separator, $this->gedcom_place)) . '</span>'; 174 } 175 176 // If a place hierarchy is a list of distinct items 177 $tmp = []; 178 foreach ($this->gedcom_place as $place) { 179 $tmp[] = '<span dir="auto">' . e($place) . '</span>'; 180 } 181 182 return implode(I18N::$list_separator, $tmp); 183 } 184 185 /** 186 * For lists and charts, where the full name won’t fit. 187 * 188 * @return string 189 */ 190 public function getShortName() 191 { 192 $SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES'); 193 194 if ($SHOW_PEDIGREE_PLACES >= count($this->gedcom_place)) { 195 // A short place name - no need to abbreviate 196 return $this->getFullName(); 197 } 198 199 // Abbreviate the place name, for lists 200 if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) { 201 // The *last* $SHOW_PEDIGREE_PLACES components 202 $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, -$SHOW_PEDIGREE_PLACES)); 203 } else { 204 // The *first* $SHOW_PEDIGREE_PLACES components 205 $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 0, $SHOW_PEDIGREE_PLACES)); 206 } 207 208 // Add a tool-tip showing the full name 209 return '<span title="' . e($this->getGedcomName()) . '" dir="auto">' . e($short_name) . '</span>'; 210 } 211 212 /** 213 * For the Place hierarchy "list all" option 214 * 215 * @return string 216 */ 217 public function getReverseName(): string 218 { 219 $tmp = []; 220 foreach (array_reverse($this->gedcom_place) as $place) { 221 $tmp[] = '<span dir="auto">' . e($place) . '</span>'; 222 } 223 224 return implode(I18N::$list_separator, $tmp); 225 } 226 227 /** 228 * Fetch all places from the database. 229 * 230 * @param Tree $tree 231 * 232 * @return Place[] 233 */ 234 public static function allPlaces(Tree $tree): array 235 { 236 $places = []; 237 $rows = 238 Database::prepare( 239 "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)" . 240 " FROM `##places` AS p1" . 241 " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" . 242 " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" . 243 " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" . 244 " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" . 245 " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" . 246 " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" . 247 " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" . 248 " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" . 249 " WHERE p1.p_file = :tree_id" . 250 " 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" 251 ) 252 ->execute([ 253 'tree_id' => $tree->getTreeId(), 254 'collate' => I18N::collation(), 255 ])->fetchOneColumn(); 256 foreach ($rows as $row) { 257 $places[] = new self($row, $tree); 258 } 259 260 return $places; 261 } 262 263 /** 264 * Search for a place name. 265 * 266 * @param string $filter 267 * @param Tree $tree 268 * 269 * @return Place[] 270 */ 271 public static function findPlaces($filter, Tree $tree): array 272 { 273 $places = []; 274 $rows = 275 Database::prepare( 276 "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)" . 277 " FROM `##places` AS p1" . 278 " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" . 279 " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" . 280 " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" . 281 " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" . 282 " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" . 283 " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" . 284 " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" . 285 " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" . 286 " 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" . 287 " 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" 288 )->execute([ 289 'filter_1' => preg_quote($filter), 290 'filter_2' => preg_quote($filter), 291 'tree_id' => $tree->getTreeId(), 292 'collation' => I18N::collation(), 293 ])->fetchOneColumn(); 294 foreach ($rows as $row) { 295 $places[] = new self($row, $tree); 296 } 297 298 return $places; 299 } 300} 301