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