xref: /webtrees/app/Place.php (revision dd04c183d8beed05be2226b30b7dda485ea4538a)
1a25f0a04SGreg Roach<?php
2*dd04c183SGreg Roachnamespace Fisharebest\Webtrees;
3a25f0a04SGreg Roach
4a25f0a04SGreg Roach/**
5a25f0a04SGreg Roach * webtrees: online genealogy
6a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10a25f0a04SGreg Roach * (at your option) any later version.
11a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14a25f0a04SGreg Roach * GNU General Public License for more details.
15a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17a25f0a04SGreg Roach */
18a25f0a04SGreg Roach
19a25f0a04SGreg Roach/**
20a25f0a04SGreg Roach * Class Place - Gedcom Place functionality.
21a25f0a04SGreg Roach */
22a25f0a04SGreg Roachclass Place {
23a25f0a04SGreg Roach	const GEDCOM_SEPARATOR = ', ';
24a25f0a04SGreg Roach	private $gedcom_place; // e.g. array("Westminster", "London", "England")
25a25f0a04SGreg Roach	private $gedcom_id; // We may have the same place in different trees
26a25f0a04SGreg Roach
27a25f0a04SGreg Roach	/**
28a25f0a04SGreg Roach	 * @param string  $gedcom_place
29a25f0a04SGreg Roach	 * @param integer $gedcom_id
30a25f0a04SGreg Roach	 */
31a25f0a04SGreg Roach	public function __construct($gedcom_place, $gedcom_id) {
32a25f0a04SGreg Roach		if ($gedcom_place) {
33a25f0a04SGreg Roach			$this->gedcom_place = explode(self::GEDCOM_SEPARATOR, $gedcom_place);
34a25f0a04SGreg Roach		} else {
35a25f0a04SGreg Roach			// Empty => "Top level"
36a25f0a04SGreg Roach			$this->gedcom_place = array();
37a25f0a04SGreg Roach		}
38a25f0a04SGreg Roach		$this->gedcom_id = $gedcom_id;
39a25f0a04SGreg Roach	}
40a25f0a04SGreg Roach
41a25f0a04SGreg Roach	/**
42a25f0a04SGreg Roach	 * @return integer
43a25f0a04SGreg Roach	 */
44a25f0a04SGreg Roach	public function getPlaceId() {
45a25f0a04SGreg Roach		$place_id = 0;
46a25f0a04SGreg Roach		foreach (array_reverse($this->gedcom_place) as $place) {
47a25f0a04SGreg Roach			$place_id = Database::prepare(
48a25f0a04SGreg Roach				"SELECT SQL_CACHE p_id FROM `##places` WHERE p_parent_id = :parent_id AND p_place = :place AND p_file = :tree_id"
49a25f0a04SGreg Roach			)->execute(array(
50a25f0a04SGreg Roach				'parent_id' => $place_id,
51a25f0a04SGreg Roach				'place'     => $place,
52a25f0a04SGreg Roach				'tree_id'   => $this->gedcom_id,
53a25f0a04SGreg Roach			))->fetchOne();
54a25f0a04SGreg Roach		}
55a25f0a04SGreg Roach
56a25f0a04SGreg Roach		return $place_id;
57a25f0a04SGreg Roach	}
58a25f0a04SGreg Roach
59a25f0a04SGreg Roach	/**
60a25f0a04SGreg Roach	 * @return Place
61a25f0a04SGreg Roach	 */
62a25f0a04SGreg Roach	public function getParentPlace() {
63a25f0a04SGreg Roach		return new Place(implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 1)), $this->gedcom_id);
64a25f0a04SGreg Roach	}
65a25f0a04SGreg Roach
66a25f0a04SGreg Roach	/**
67a25f0a04SGreg Roach	 * @return Place[]
68a25f0a04SGreg Roach	 */
69a25f0a04SGreg Roach	public function getChildPlaces() {
70a25f0a04SGreg Roach		$children = array();
71a25f0a04SGreg Roach		if ($this->getPlaceId()) {
72a25f0a04SGreg Roach			$parent_text = self::GEDCOM_SEPARATOR . $this->getGedcomName();
73a25f0a04SGreg Roach		} else {
74a25f0a04SGreg Roach			$parent_text = '';
75a25f0a04SGreg Roach		}
76a25f0a04SGreg Roach
77a25f0a04SGreg Roach		$rows = Database::prepare(
78a25f0a04SGreg Roach			"SELECT SQL_CACHE p_place FROM `##places`" .
79a25f0a04SGreg Roach			" WHERE p_parent_id = :parent_id AND p_file = :tree_id" .
80a25f0a04SGreg Roach			" ORDER BY p_place COLLATE :collation"
81a25f0a04SGreg Roach		)->execute(array(
82a25f0a04SGreg Roach			'parent_id' => $this->getPlaceId(),
83a25f0a04SGreg Roach			'tree_id'   => $this->gedcom_id,
84a25f0a04SGreg Roach			'collation' => I18N::$collation,
85a25f0a04SGreg Roach		))->fetchOneColumn();
86a25f0a04SGreg Roach		foreach ($rows as $row) {
87a25f0a04SGreg Roach			$children[] = new Place($row . $parent_text, $this->gedcom_id);
88a25f0a04SGreg Roach		}
89a25f0a04SGreg Roach
90a25f0a04SGreg Roach		return $children;
91a25f0a04SGreg Roach	}
92a25f0a04SGreg Roach
93a25f0a04SGreg Roach	/**
94a25f0a04SGreg Roach	 * @return string
95a25f0a04SGreg Roach	 */
96a25f0a04SGreg Roach	public function getURL() {
97a25f0a04SGreg Roach		$url = 'placelist.php';
98a25f0a04SGreg Roach		foreach (array_reverse($this->gedcom_place) as $n => $place) {
99a25f0a04SGreg Roach			$url .= $n ? '&amp;' : '?';
100a25f0a04SGreg Roach			$url .= 'parent%5B%5D=' . rawurlencode($place);
101a25f0a04SGreg Roach		}
102a25f0a04SGreg Roach		$url .= '&amp;ged=' . rawurlencode(get_gedcom_from_id($this->gedcom_id));
103a25f0a04SGreg Roach
104a25f0a04SGreg Roach		return $url;
105a25f0a04SGreg Roach	}
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach	/**
108a25f0a04SGreg Roach	 * @return string
109a25f0a04SGreg Roach	 */
110a25f0a04SGreg Roach	public function getGedcomName() {
111a25f0a04SGreg Roach		return implode(self::GEDCOM_SEPARATOR, $this->gedcom_place);
112a25f0a04SGreg Roach	}
113a25f0a04SGreg Roach
114a25f0a04SGreg Roach	/**
115a25f0a04SGreg Roach	 * @return string
116a25f0a04SGreg Roach	 */
117a25f0a04SGreg Roach	public function getPlaceName() {
118a25f0a04SGreg Roach		$place = reset($this->gedcom_place);
119a25f0a04SGreg Roach
120a25f0a04SGreg Roach		return $place ? '<span dir="auto">' . Filter::escapeHtml($place) . '</span>' : I18N::translate('unknown');
121a25f0a04SGreg Roach	}
122a25f0a04SGreg Roach
123a25f0a04SGreg Roach	/**
124a25f0a04SGreg Roach	 * @return bool
125a25f0a04SGreg Roach	 */
126a25f0a04SGreg Roach	public function isEmpty() {
127a25f0a04SGreg Roach		return empty($this->gedcom_place);
128a25f0a04SGreg Roach	}
129a25f0a04SGreg Roach
130a25f0a04SGreg Roach	/**
131a25f0a04SGreg Roach	 * @return string
132a25f0a04SGreg Roach	 */
133a25f0a04SGreg Roach	public function getFullName() {
134a25f0a04SGreg Roach		if (true) {
135a25f0a04SGreg Roach			// If a place hierarchy is a single entity
136a25f0a04SGreg Roach			return '<span dir="auto">' . Filter::escapeHtml(implode(I18N::$list_separator, $this->gedcom_place)) . '</span>';
137a25f0a04SGreg Roach		} else {
138a25f0a04SGreg Roach			// If a place hierarchy is a list of distinct items
139a25f0a04SGreg Roach			$tmp = array();
140a25f0a04SGreg Roach			foreach ($this->gedcom_place as $place) {
141a25f0a04SGreg Roach				$tmp[] = '<span dir="auto">' . Filter::escapeHtml($place) . '</span>';
142a25f0a04SGreg Roach			}
143a25f0a04SGreg Roach
144a25f0a04SGreg Roach			return implode(I18N::$list_separator, $tmp);
145a25f0a04SGreg Roach		}
146a25f0a04SGreg Roach	}
147a25f0a04SGreg Roach
148a25f0a04SGreg Roach	/**
149a25f0a04SGreg Roach	 * For lists and charts, where the full name won’t fit.
150a25f0a04SGreg Roach	 *
151a25f0a04SGreg Roach	 * @return string
152a25f0a04SGreg Roach	 */
153a25f0a04SGreg Roach	public function getShortName() {
154a25f0a04SGreg Roach		global $SHOW_PEDIGREE_PLACES, $SHOW_PEDIGREE_PLACES_SUFFIX;
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach		if ($SHOW_PEDIGREE_PLACES >= count($this->gedcom_place)) {
157a25f0a04SGreg Roach			// A short place name - no need to abbreviate
158a25f0a04SGreg Roach			return $this->getFullName();
159a25f0a04SGreg Roach		} else {
160a25f0a04SGreg Roach			// Abbreviate the place name, for lists
161a25f0a04SGreg Roach			if ($SHOW_PEDIGREE_PLACES_SUFFIX) {
162a25f0a04SGreg Roach				// The *last* $SHOW_PEDIGREE_PLACES components
163a25f0a04SGreg Roach				$short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, -$SHOW_PEDIGREE_PLACES));
164a25f0a04SGreg Roach			} else {
165a25f0a04SGreg Roach				// The *first* $SHOW_PEDIGREE_PLACES components
166a25f0a04SGreg Roach				$short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 0, $SHOW_PEDIGREE_PLACES));
167a25f0a04SGreg Roach			}
168a25f0a04SGreg Roach			// Add a tool-tip showing the full name
169a25f0a04SGreg Roach			return '<span title="' . Filter::escapeHtml($this->getGedcomName()) . '" dir="auto">' . Filter::escapeHtml($short_name) . '</span>';
170a25f0a04SGreg Roach		}
171a25f0a04SGreg Roach	}
172a25f0a04SGreg Roach
173a25f0a04SGreg Roach	/**
174a25f0a04SGreg Roach	 * For the "view all" option of placelist.php and find.php
175a25f0a04SGreg Roach	 *
176a25f0a04SGreg Roach	 * @return string
177a25f0a04SGreg Roach	 */
178a25f0a04SGreg Roach	public function getReverseName() {
179a25f0a04SGreg Roach		$tmp = array();
180a25f0a04SGreg Roach		foreach (array_reverse($this->gedcom_place) as $place) {
181a25f0a04SGreg Roach			$tmp[] = '<span dir="auto">' . Filter::escapeHtml($place) . '</span>';
182a25f0a04SGreg Roach		}
183a25f0a04SGreg Roach
184a25f0a04SGreg Roach		return implode(I18N::$list_separator, $tmp);
185a25f0a04SGreg Roach	}
186a25f0a04SGreg Roach
187a25f0a04SGreg Roach	/**
188a25f0a04SGreg Roach	 * @param integer $gedcom_id
189a25f0a04SGreg Roach	 *
190a25f0a04SGreg Roach	 * @return string[]
191a25f0a04SGreg Roach	 */
192a25f0a04SGreg Roach	public static function allPlaces($gedcom_id) {
193a25f0a04SGreg Roach		$places = array();
194a25f0a04SGreg Roach		$rows =
195a25f0a04SGreg Roach			Database::prepare(
196a25f0a04SGreg Roach				"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)" .
197a25f0a04SGreg Roach				" FROM      `##places` AS p1" .
198a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p2 ON (p1.p_parent_id=p2.p_id)" .
199a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p3 ON (p2.p_parent_id=p3.p_id)" .
200a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p4 ON (p3.p_parent_id=p4.p_id)" .
201a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p5 ON (p4.p_parent_id=p5.p_id)" .
202a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p6 ON (p5.p_parent_id=p6.p_id)" .
203a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p7 ON (p6.p_parent_id=p7.p_id)" .
204a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p8 ON (p7.p_parent_id=p8.p_id)" .
205a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p9 ON (p8.p_parent_id=p9.p_id)" .
206a25f0a04SGreg Roach				" WHERE p1.p_file=?" .
207a25f0a04SGreg 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 '" . I18N::$collation . "'"
208a25f0a04SGreg Roach			)
209a25f0a04SGreg Roach			->execute(array($gedcom_id))
210a25f0a04SGreg Roach			->fetchOneColumn();
211a25f0a04SGreg Roach		foreach ($rows as $row) {
212a25f0a04SGreg Roach			$places[] = new Place($row, $gedcom_id);
213a25f0a04SGreg Roach		}
214a25f0a04SGreg Roach		return $places;
215a25f0a04SGreg Roach	}
216a25f0a04SGreg Roach
217a25f0a04SGreg Roach	/**
218a25f0a04SGreg Roach	 * @param string  $filter
219a25f0a04SGreg Roach	 * @param integer $gedcom_id
220a25f0a04SGreg Roach	 *
221a25f0a04SGreg Roach	 * @return Place[]
222a25f0a04SGreg Roach	 */
223a25f0a04SGreg Roach	public static function findPlaces($filter, $gedcom_id) {
224a25f0a04SGreg Roach		$places = array();
225a25f0a04SGreg Roach		$rows =
226a25f0a04SGreg Roach			Database::prepare(
227a25f0a04SGreg Roach				"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)" .
228a25f0a04SGreg Roach				" FROM      `##places` AS p1" .
229a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p2 ON (p1.p_parent_id=p2.p_id)" .
230a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p3 ON (p2.p_parent_id=p3.p_id)" .
231a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p4 ON (p3.p_parent_id=p4.p_id)" .
232a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p5 ON (p4.p_parent_id=p5.p_id)" .
233a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p6 ON (p5.p_parent_id=p6.p_id)" .
234a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p7 ON (p6.p_parent_id=p7.p_id)" .
235a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p8 ON (p7.p_parent_id=p8.p_id)" .
236a25f0a04SGreg Roach				" LEFT JOIN `##places` AS p9 ON (p8.p_parent_id=p9.p_id)" .
237a25f0a04SGreg 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('%', ?, '%') 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('%,%', ?, '%') AND p1.p_file=?" .
238a25f0a04SGreg 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 '" . I18N::$collation . "'"
239a25f0a04SGreg Roach			)
240a25f0a04SGreg Roach			->execute(array($filter, preg_quote($filter), $gedcom_id))
241a25f0a04SGreg Roach			->fetchOneColumn();
242a25f0a04SGreg Roach		foreach ($rows as $row) {
243a25f0a04SGreg Roach			$places[] = new Place($row, $gedcom_id);
244a25f0a04SGreg Roach		}
245a25f0a04SGreg Roach		return $places;
246a25f0a04SGreg Roach	}
247a25f0a04SGreg Roach}
248