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