summaryrefslogtreecommitdiff
path: root/app/Place.php
blob: 6da6d76dd42ba5402b36e934ce8fae1a8010d4d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2017 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees;

/**
 * A GEDCOM place (PLAC) object.
 */
class Place {
	const GEDCOM_SEPARATOR = ', ';

	/** @var string[] e.g. array('Westminster', 'London', 'England') */
	private $gedcom_place;

	/** @var Tree We may have the same place name in different trees. */
	private $tree;

	/**
	 * Create a place.
	 *
	 * @param string $gedcom_place
	 * @param Tree   $tree
	 */
	public function __construct($gedcom_place, Tree $tree) {
		if ($gedcom_place) {
			$this->gedcom_place = explode(self::GEDCOM_SEPARATOR, $gedcom_place);
		} else {
			// Empty => "Top level"
			$this->gedcom_place = [];
		}
		$this->tree = $tree;
	}

	/**
	 * Get the identifier for a place.
	 *
	 * @return int
	 */
	public function getPlaceId() {
		$place_id = 0;
		foreach (array_reverse($this->gedcom_place) as $place) {
			$place_id = Database::prepare(
				"SELECT SQL_CACHE p_id FROM `##places` WHERE p_parent_id = :parent_id AND p_place = :place AND p_file = :tree_id"
			)->execute([
				'parent_id' => $place_id,
				'place'     => $place,
				'tree_id'   => $this->tree->getTreeId(),
			])->fetchOne();
		}

		return $place_id;
	}

	/**
	 * Get the higher level place.
	 *
	 * @return Place
	 */
	public function getParentPlace() {
		return new self(implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 1)), $this->tree);
	}

	/**
	 * Get the lower level places.
	 *
	 * @return Place[]
	 */
	public function getChildPlaces() {
		$children = [];
		if ($this->getPlaceId()) {
			$parent_text = self::GEDCOM_SEPARATOR . $this->getGedcomName();
		} else {
			$parent_text = '';
		}

		$rows = Database::prepare(
			"SELECT SQL_CACHE p_place FROM `##places`" .
			" WHERE p_parent_id = :parent_id AND p_file = :tree_id" .
			" ORDER BY p_place COLLATE :collation"
		)->execute([
			'parent_id' => $this->getPlaceId(),
			'tree_id'   => $this->tree->getTreeId(),
			'collation' => I18N::collation(),
		])->fetchOneColumn();
		foreach ($rows as $row) {
			$children[] = new self($row . $parent_text, $this->tree);
		}

		return $children;
	}

	/**
	 * Create a URL to the place-hierarchy page.
	 *
	 * @return string
	 */
	public function getURL() {
		$url = 'placelist.php';
		foreach (array_reverse($this->gedcom_place) as $n => $place) {
			$url .= $n ? '&amp;' : '?';
			$url .= 'parent%5B%5D=' . rawurlencode($place);
		}
		$url .= '&amp;ged=' . $this->tree->getNameUrl();

		return $url;
	}

	/**
	 * Format this name for GEDCOM data.
	 *
	 * @return string
	 */
	public function getGedcomName() {
		return implode(self::GEDCOM_SEPARATOR, $this->gedcom_place);
	}

	/**
	 * Format this place for display on screen.
	 *
	 * @return string
	 */
	public function getPlaceName() {
		$place = reset($this->gedcom_place);

		return $place ? '<span dir="auto">' . Html::escape($place) . '</span>' : I18N::translate('unknown');
	}

	/**
	 * Is this a null/empty/missing/invalid place?
	 *
	 * @return bool
	 */
	public function isEmpty() {
		return empty($this->gedcom_place);
	}

	/**
	 * Generate the place name for display, including the full hierarchy.
	 *
	 * @return string
	 */
	public function getFullName() {
		if (true) {
			// If a place hierarchy is a single entity
			return '<span dir="auto">' . Html::escape(implode(I18N::$list_separator, $this->gedcom_place)) . '</span>';
		} else {
			// If a place hierarchy is a list of distinct items
			$tmp = [];
			foreach ($this->gedcom_place as $place) {
				$tmp[] = '<span dir="auto">' . Html::escape($place) . '</span>';
			}

			return implode(I18N::$list_separator, $tmp);
		}
	}

	/**
	 * For lists and charts, where the full name won’t fit.
	 *
	 * @return string
	 */
	public function getShortName() {
		$SHOW_PEDIGREE_PLACES = $this->tree->getPreference('SHOW_PEDIGREE_PLACES');

		if ($SHOW_PEDIGREE_PLACES >= count($this->gedcom_place)) {
			// A short place name - no need to abbreviate
			return $this->getFullName();
		} else {
			// Abbreviate the place name, for lists
			if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
				// The *last* $SHOW_PEDIGREE_PLACES components
				$short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, -$SHOW_PEDIGREE_PLACES));
			} else {
				// The *first* $SHOW_PEDIGREE_PLACES components
				$short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 0, $SHOW_PEDIGREE_PLACES));
			}
			// Add a tool-tip showing the full name
			return '<span title="' . Html::escape($this->getGedcomName()) . '" dir="auto">' . Html::escape($short_name) . '</span>';
		}
	}

	/**
	 * For the "view all" option of placelist.phpp
	 *
	 * @return string
	 */
	public function getReverseName() {
		$tmp = [];
		foreach (array_reverse($this->gedcom_place) as $place) {
			$tmp[] = '<span dir="auto">' . Html::escape($place) . '</span>';
		}

		return implode(I18N::$list_separator, $tmp);
	}

	/**
	 * Fetch all places from the database.
	 *
	 * @param Tree $tree
	 *
	 * @return string[]
	 */
	public static function allPlaces(Tree $tree) {
		$places = [];
		$rows   =
			Database::prepare(
				"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)" .
				" FROM      `##places` AS p1" .
				" LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" .
				" LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" .
				" LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" .
				" LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" .
				" LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" .
				" LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" .
				" LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" .
				" LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" .
				" WHERE p1.p_file = :tree_id" .
				" 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"
			)
			->execute([
				'tree_id' => $tree->getTreeId(),
				'collate' => I18N::collation(),
			])->fetchOneColumn();
		foreach ($rows as $row) {
			$places[] = new self($row, $tree);
		}

		return $places;
	}

	/**
	 * Search for a place name.
	 *
	 * @param string  $filter
	 * @param Tree    $tree
	 *
	 * @return Place[]
	 */
	public static function findPlaces($filter, Tree $tree) {
		$places = [];
		$rows   =
			Database::prepare(
				"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)" .
				" FROM      `##places` AS p1" .
				" LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" .
				" LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" .
				" LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" .
				" LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" .
				" LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" .
				" LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" .
				" LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" .
				" LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" .
				" 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" .
				" 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"
			)->execute([
				'filter_1'  => preg_quote($filter),
				'filter_2'  => preg_quote($filter),
				'tree_id'   => $tree->getTreeId(),
				'collation' => I18N::collation(),
			])->fetchOneColumn();
		foreach ($rows as $row) {
			$places[] = new self($row, $tree);
		}

		return $places;
	}
}