xref: /webtrees/app/Place.php (revision e5588fb099ce26dcdcb7faeb13861818cd9a4306)
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 */
21a25f0a04SGreg Roachclass Place {
22a25f0a04SGreg Roach	const GEDCOM_SEPARATOR = ', ';
2384caa210SGreg Roach
2476692c8bSGreg Roach	/** @var string[] e.g. array('Westminster', 'London', 'England') */
2584caa210SGreg Roach	private $gedcom_place;
2684caa210SGreg Roach
2776692c8bSGreg Roach	/** @var Tree We may have the same place name in different trees. */
2884caa210SGreg Roach	private $tree;
29a25f0a04SGreg Roach
30a25f0a04SGreg Roach	/**
3176692c8bSGreg Roach	 * Create a place.
3276692c8bSGreg Roach	 *
33a25f0a04SGreg Roach	 * @param string $gedcom_place
3484caa210SGreg Roach	 * @param Tree   $tree
35a25f0a04SGreg Roach	 */
362769ecbeSGreg Roach	public function __construct($gedcom_place, Tree $tree) {
37b20ddbf9SGreg Roach		if ($gedcom_place === '') {
3813abd6f3SGreg Roach			$this->gedcom_place = [];
39b20ddbf9SGreg Roach		} else {
40b20ddbf9SGreg Roach			$this->gedcom_place = explode(self::GEDCOM_SEPARATOR, $gedcom_place);
41a25f0a04SGreg Roach		}
4264b16745SGreg Roach		$this->tree = $tree;
43a25f0a04SGreg Roach	}
44a25f0a04SGreg Roach
45a25f0a04SGreg Roach	/**
4616d0b7f7SRico Sonntag	 * Extract the country (last part) of a place name.
4716d0b7f7SRico Sonntag	 *
4816d0b7f7SRico Sonntag	 * @return string - e.g. "England"
4916d0b7f7SRico Sonntag	 */
508d68cabeSGreg Roach	public function lastPart() {
5116d0b7f7SRico Sonntag		return end($this->gedcom_place);
5216d0b7f7SRico Sonntag	}
5316d0b7f7SRico Sonntag
5416d0b7f7SRico Sonntag	/**
5576692c8bSGreg Roach	 * Get the identifier for a place.
5676692c8bSGreg Roach	 *
57cbc1590aSGreg Roach	 * @return int
58a25f0a04SGreg Roach	 */
59a25f0a04SGreg Roach	public function getPlaceId() {
60a25f0a04SGreg Roach		$place_id = 0;
61a25f0a04SGreg Roach		foreach (array_reverse($this->gedcom_place) as $place) {
62a25f0a04SGreg Roach			$place_id = Database::prepare(
63*e5588fb0SGreg Roach				"SELECT p_id FROM `##places` WHERE p_parent_id = :parent_id AND p_place = :place AND p_file = :tree_id"
6413abd6f3SGreg Roach			)->execute([
65a25f0a04SGreg Roach				'parent_id' => $place_id,
66a25f0a04SGreg Roach				'place'     => $place,
6764b16745SGreg Roach				'tree_id'   => $this->tree->getTreeId(),
6813abd6f3SGreg Roach			])->fetchOne();
69a25f0a04SGreg Roach		}
70a25f0a04SGreg Roach
71a25f0a04SGreg Roach		return $place_id;
72a25f0a04SGreg Roach	}
73a25f0a04SGreg Roach
74a25f0a04SGreg Roach	/**
7576692c8bSGreg Roach	 * Get the higher level place.
7676692c8bSGreg Roach	 *
77a25f0a04SGreg Roach	 * @return Place
78a25f0a04SGreg Roach	 */
79a25f0a04SGreg Roach	public function getParentPlace() {
8006ef8e02SGreg Roach		return new self(implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 1)), $this->tree);
81a25f0a04SGreg Roach	}
82a25f0a04SGreg Roach
83a25f0a04SGreg Roach	/**
8476692c8bSGreg Roach	 * Get the lower level places.
8576692c8bSGreg Roach	 *
86a25f0a04SGreg Roach	 * @return Place[]
87a25f0a04SGreg Roach	 */
88a25f0a04SGreg Roach	public function getChildPlaces() {
8913abd6f3SGreg Roach		$children = [];
90a25f0a04SGreg Roach		if ($this->getPlaceId()) {
91a25f0a04SGreg Roach			$parent_text = self::GEDCOM_SEPARATOR . $this->getGedcomName();
92a25f0a04SGreg Roach		} else {
93a25f0a04SGreg Roach			$parent_text = '';
94a25f0a04SGreg Roach		}
95a25f0a04SGreg Roach
96a25f0a04SGreg Roach		$rows = Database::prepare(
97*e5588fb0SGreg Roach			"SELECT p_place FROM `##places`" .
98a25f0a04SGreg Roach			" WHERE p_parent_id = :parent_id AND p_file = :tree_id" .
99a25f0a04SGreg Roach			" ORDER BY p_place COLLATE :collation"
10013abd6f3SGreg Roach		)->execute([
101a25f0a04SGreg Roach			'parent_id' => $this->getPlaceId(),
10264b16745SGreg Roach			'tree_id'   => $this->tree->getTreeId(),
10381088023SGreg Roach			'collation' => I18N::collation(),
10413abd6f3SGreg Roach		])->fetchOneColumn();
105a25f0a04SGreg Roach		foreach ($rows as $row) {
10606ef8e02SGreg Roach			$children[] = new self($row . $parent_text, $this->tree);
107a25f0a04SGreg Roach		}
108a25f0a04SGreg Roach
109a25f0a04SGreg Roach		return $children;
110a25f0a04SGreg Roach	}
111a25f0a04SGreg Roach
112a25f0a04SGreg Roach	/**
11376692c8bSGreg Roach	 * Create a URL to the place-hierarchy page.
11476692c8bSGreg Roach	 *
115a25f0a04SGreg Roach	 * @return string
116a25f0a04SGreg Roach	 */
117a25f0a04SGreg Roach	public function getURL() {
11801a6db52SGreg Roach		return e(Html::url('placelist.php', [
11901a6db52SGreg Roach			'parent' => array_reverse($this->gedcom_place),
12001a6db52SGreg Roach			'ged' => $this->tree->getName(),
12101a6db52SGreg Roach		]));
122a25f0a04SGreg Roach	}
123a25f0a04SGreg Roach
124a25f0a04SGreg Roach	/**
12576692c8bSGreg Roach	 * Format this name for GEDCOM data.
12676692c8bSGreg Roach	 *
127a25f0a04SGreg Roach	 * @return string
128a25f0a04SGreg Roach	 */
129a25f0a04SGreg Roach	public function getGedcomName() {
130a25f0a04SGreg Roach		return implode(self::GEDCOM_SEPARATOR, $this->gedcom_place);
131a25f0a04SGreg Roach	}
132a25f0a04SGreg Roach
133a25f0a04SGreg Roach	/**
13476692c8bSGreg Roach	 * Format this place for display on screen.
13576692c8bSGreg Roach	 *
136a25f0a04SGreg Roach	 * @return string
137a25f0a04SGreg Roach	 */
138a25f0a04SGreg Roach	public function getPlaceName() {
139a25f0a04SGreg Roach		$place = reset($this->gedcom_place);
140a25f0a04SGreg Roach
141d53324c9SGreg Roach		return $place ? '<span dir="auto">' . e($place) . '</span>' : I18N::translate('unknown');
142a25f0a04SGreg Roach	}
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach	/**
14576692c8bSGreg Roach	 * Is this a null/empty/missing/invalid place?
14676692c8bSGreg Roach	 *
147a25f0a04SGreg Roach	 * @return bool
148a25f0a04SGreg Roach	 */
149a25f0a04SGreg Roach	public function isEmpty() {
150a25f0a04SGreg Roach		return empty($this->gedcom_place);
151a25f0a04SGreg Roach	}
152a25f0a04SGreg Roach
153a25f0a04SGreg Roach	/**
15476692c8bSGreg Roach	 * Generate the place name for display, including the full hierarchy.
15576692c8bSGreg Roach	 *
156a25f0a04SGreg Roach	 * @return string
157a25f0a04SGreg Roach	 */
158a25f0a04SGreg Roach	public function getFullName() {
159a25f0a04SGreg Roach		if (true) {
160a25f0a04SGreg Roach			// If a place hierarchy is a single entity
161d53324c9SGreg Roach			return '<span dir="auto">' . e(implode(I18N::$list_separator, $this->gedcom_place)) . '</span>';
162a25f0a04SGreg Roach		} else {
163a25f0a04SGreg Roach			// If a place hierarchy is a list of distinct items
16413abd6f3SGreg Roach			$tmp = [];
165a25f0a04SGreg Roach			foreach ($this->gedcom_place as $place) {
166d53324c9SGreg Roach				$tmp[] = '<span dir="auto">' . e($place) . '</span>';
167a25f0a04SGreg Roach			}
168a25f0a04SGreg Roach
169a25f0a04SGreg Roach			return implode(I18N::$list_separator, $tmp);
170a25f0a04SGreg Roach		}
171a25f0a04SGreg Roach	}
172a25f0a04SGreg Roach
173a25f0a04SGreg Roach	/**
174a25f0a04SGreg Roach	 * For lists and charts, where the full name won’t fit.
175a25f0a04SGreg Roach	 *
176a25f0a04SGreg Roach	 * @return string
177a25f0a04SGreg Roach	 */
178a25f0a04SGreg Roach	public function getShortName() {
179a99c6938SGreg Roach		$SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
180a25f0a04SGreg Roach
181a25f0a04SGreg Roach		if ($SHOW_PEDIGREE_PLACES >= count($this->gedcom_place)) {
182a25f0a04SGreg Roach			// A short place name - no need to abbreviate
183a25f0a04SGreg Roach			return $this->getFullName();
184a25f0a04SGreg Roach		} else {
185a25f0a04SGreg Roach			// Abbreviate the place name, for lists
18664b16745SGreg Roach			if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
187a25f0a04SGreg Roach				// The *last* $SHOW_PEDIGREE_PLACES components
188a25f0a04SGreg Roach				$short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, -$SHOW_PEDIGREE_PLACES));
189a25f0a04SGreg Roach			} else {
190a25f0a04SGreg Roach				// The *first* $SHOW_PEDIGREE_PLACES components
191a25f0a04SGreg Roach				$short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 0, $SHOW_PEDIGREE_PLACES));
192a25f0a04SGreg Roach			}
193a25f0a04SGreg Roach			// Add a tool-tip showing the full name
194d53324c9SGreg Roach			return '<span title="' . e($this->getGedcomName()) . '" dir="auto">' . e($short_name) . '</span>';
195a25f0a04SGreg Roach		}
196a25f0a04SGreg Roach	}
197a25f0a04SGreg Roach
198a25f0a04SGreg Roach	/**
19915d603e7SGreg Roach	 * For the "view all" option of placelist.phpp
200a25f0a04SGreg Roach	 *
201a25f0a04SGreg Roach	 * @return string
202a25f0a04SGreg Roach	 */
203a25f0a04SGreg Roach	public function getReverseName() {
20413abd6f3SGreg Roach		$tmp = [];
205a25f0a04SGreg Roach		foreach (array_reverse($this->gedcom_place) as $place) {
206d53324c9SGreg Roach			$tmp[] = '<span dir="auto">' . e($place) . '</span>';
207a25f0a04SGreg Roach		}
208a25f0a04SGreg Roach
209a25f0a04SGreg Roach		return implode(I18N::$list_separator, $tmp);
210a25f0a04SGreg Roach	}
211a25f0a04SGreg Roach
212a25f0a04SGreg Roach	/**
21376692c8bSGreg Roach	 * Fetch all places from the database.
21476692c8bSGreg Roach	 *
21584caa210SGreg Roach	 * @param Tree $tree
216a25f0a04SGreg Roach	 *
2174080d558SGreg Roach	 * @return Place[]
218a25f0a04SGreg Roach	 */
219703a96f4SGreg Roach	public static function allPlaces(Tree $tree) {
22013abd6f3SGreg Roach		$places = [];
221a25f0a04SGreg Roach		$rows   =
222a25f0a04SGreg Roach			Database::prepare(
223*e5588fb0SGreg 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)" .
224a25f0a04SGreg Roach				" FROM      `##places` AS p1" .
225a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" .
226a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" .
227a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" .
228a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" .
229a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" .
230a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" .
231a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" .
232a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" .
23384caa210SGreg Roach				" WHERE p1.p_file = :tree_id" .
234288566a3SGreg 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"
235a25f0a04SGreg Roach			)
23613abd6f3SGreg Roach			->execute([
237288566a3SGreg Roach				'tree_id' => $tree->getTreeId(),
23881088023SGreg Roach				'collate' => I18N::collation(),
23913abd6f3SGreg Roach			])->fetchOneColumn();
240a25f0a04SGreg Roach		foreach ($rows as $row) {
24106ef8e02SGreg Roach			$places[] = new self($row, $tree);
242a25f0a04SGreg Roach		}
243cbc1590aSGreg Roach
244a25f0a04SGreg Roach		return $places;
245a25f0a04SGreg Roach	}
246a25f0a04SGreg Roach
247a25f0a04SGreg Roach	/**
24876692c8bSGreg Roach	 * Search for a place name.
24976692c8bSGreg Roach	 *
250a25f0a04SGreg Roach	 * @param string  $filter
25184caa210SGreg Roach	 * @param Tree    $tree
252a25f0a04SGreg Roach	 *
2534080d558SGreg Roach	 * @return Place[]
254a25f0a04SGreg Roach	 */
2552769ecbeSGreg Roach	public static function findPlaces($filter, Tree $tree) {
25613abd6f3SGreg Roach		$places = [];
257a25f0a04SGreg Roach		$rows   =
258a25f0a04SGreg Roach			Database::prepare(
259*e5588fb0SGreg 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)" .
260a25f0a04SGreg Roach				" FROM      `##places` AS p1" .
261a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" .
262a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" .
263a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" .
264a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" .
265a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" .
266a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" .
267a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" .
268a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" .
26984caa210SGreg 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" .
27081088023SGreg 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"
27113abd6f3SGreg Roach			)->execute([
27284caa210SGreg Roach				'filter_1'  => preg_quote($filter),
27384caa210SGreg Roach				'filter_2'  => preg_quote($filter),
27481088023SGreg Roach				'tree_id'   => $tree->getTreeId(),
27581088023SGreg Roach				'collation' => I18N::collation(),
27613abd6f3SGreg Roach			])->fetchOneColumn();
277a25f0a04SGreg Roach		foreach ($rows as $row) {
27806ef8e02SGreg Roach			$places[] = new self($row, $tree);
279a25f0a04SGreg Roach		}
280cbc1590aSGreg Roach
281a25f0a04SGreg Roach		return $places;
282a25f0a04SGreg Roach	}
283a25f0a04SGreg Roach}
284