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