xref: /webtrees/app/Place.php (revision 67994fb087e1b24564a780e4ae8aeff801733e35)
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 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20/**
21 * A GEDCOM place (PLAC) object.
22 */
23class Place
24{
25    const GEDCOM_SEPARATOR = ', ';
26
27    /** @var string[] e.g. array('Westminster', 'London', 'England') */
28    private $gedcom_place;
29
30    /** @var Tree We may have the same place name in different trees. */
31    private $tree;
32
33    /**
34     * Create a place.
35     *
36     * @param string $gedcom_place
37     * @param Tree   $tree
38     */
39    public function __construct($gedcom_place, Tree $tree)
40    {
41        if ($gedcom_place === '') {
42            $this->gedcom_place = [];
43        } else {
44            $this->gedcom_place = explode(self::GEDCOM_SEPARATOR, $gedcom_place);
45        }
46        $this->tree = $tree;
47    }
48
49    /**
50     * Extract the country (last part) of a place name.
51     *
52     * @return string - e.g. "England"
53     */
54    public function lastPart(): string
55    {
56        return end($this->gedcom_place);
57    }
58
59    /**
60     * Get the identifier for a place.
61     *
62     * @return int
63     */
64    public function getPlaceId(): int
65    {
66        $place_id = 0;
67
68        foreach (array_reverse($this->gedcom_place) as $place) {
69            $place_id = (int) Database::prepare(
70                "SELECT p_id FROM `##places` WHERE p_parent_id = :parent_id AND p_place = :place AND p_file = :tree_id"
71            )->execute([
72                'parent_id' => $place_id,
73                'place'     => $place,
74                'tree_id'   => $this->tree->getTreeId(),
75            ])->fetchOne();
76        }
77
78        return $place_id;
79    }
80
81    /**
82     * Get the higher level place.
83     *
84     * @return Place
85     */
86    public function getParentPlace(): Place
87    {
88        return new self(implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 1)), $this->tree);
89    }
90
91    /**
92     * Get the lower level places.
93     *
94     * @return Place[]
95     */
96    public function getChildPlaces(): array
97    {
98        $children = [];
99        if ($this->getPlaceId()) {
100            $parent_text = self::GEDCOM_SEPARATOR . $this->getGedcomName();
101        } else {
102            $parent_text = '';
103        }
104
105        $rows = Database::prepare(
106            "SELECT p_place FROM `##places`" .
107            " WHERE p_parent_id = :parent_id AND p_file = :tree_id" .
108            " ORDER BY p_place COLLATE :collation"
109        )->execute([
110            'parent_id' => $this->getPlaceId(),
111            'tree_id'   => $this->tree->getTreeId(),
112            'collation' => I18N::collation(),
113        ])->fetchOneColumn();
114        foreach ($rows as $row) {
115            $children[] = new self($row . $parent_text, $this->tree);
116        }
117
118        return $children;
119    }
120
121    /**
122     * Create a URL to the place-hierarchy page.
123     *
124     * @return string
125     */
126    public function getURL(): string
127    {
128        return e(route('place-hierarchy', [
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(): string
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(): string
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(): bool
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        }
177
178        // If a place hierarchy is a list of distinct items
179        $tmp = [];
180        foreach ($this->gedcom_place as $place) {
181            $tmp[] = '<span dir="auto">' . e($place) . '</span>';
182        }
183
184        return implode(I18N::$list_separator, $tmp);
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        }
200
201        // Abbreviate the place name, for lists
202        if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
203            // The *last* $SHOW_PEDIGREE_PLACES components
204            $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, -$SHOW_PEDIGREE_PLACES));
205        } else {
206            // The *first* $SHOW_PEDIGREE_PLACES components
207            $short_name = implode(self::GEDCOM_SEPARATOR, array_slice($this->gedcom_place, 0, $SHOW_PEDIGREE_PLACES));
208        }
209
210        // Add a tool-tip showing the full name
211        return '<span title="' . e($this->getGedcomName()) . '" dir="auto">' . e($short_name) . '</span>';
212    }
213
214    /**
215     * For the Place hierarchy "list all" option
216     *
217     * @return string
218     */
219    public function getReverseName(): string
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     */
236    public static function allPlaces(Tree $tree): array
237    {
238        $places = [];
239        $rows   =
240            Database::prepare(
241                "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)" .
242                " FROM      `##places` AS p1" .
243                " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" .
244                " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" .
245                " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" .
246                " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" .
247                " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" .
248                " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" .
249                " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" .
250                " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" .
251                " WHERE p1.p_file = :tree_id" .
252                " 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"
253            )
254                ->execute([
255                    'tree_id' => $tree->getTreeId(),
256                    'collate' => I18N::collation(),
257                ])->fetchOneColumn();
258        foreach ($rows as $row) {
259            $places[] = new self($row, $tree);
260        }
261
262        return $places;
263    }
264
265    /**
266     * Search for a place name.
267     *
268     * @param string $filter
269     * @param Tree   $tree
270     *
271     * @return Place[]
272     */
273    public static function findPlaces($filter, Tree $tree): array
274    {
275        $places = [];
276        $rows   =
277            Database::prepare(
278                "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)" .
279                " FROM      `##places` AS p1" .
280                " LEFT JOIN `##places` AS p2 ON (p1.p_parent_id = p2.p_id)" .
281                " LEFT JOIN `##places` AS p3 ON (p2.p_parent_id = p3.p_id)" .
282                " LEFT JOIN `##places` AS p4 ON (p3.p_parent_id = p4.p_id)" .
283                " LEFT JOIN `##places` AS p5 ON (p4.p_parent_id = p5.p_id)" .
284                " LEFT JOIN `##places` AS p6 ON (p5.p_parent_id = p6.p_id)" .
285                " LEFT JOIN `##places` AS p7 ON (p6.p_parent_id = p7.p_id)" .
286                " LEFT JOIN `##places` AS p8 ON (p7.p_parent_id = p8.p_id)" .
287                " LEFT JOIN `##places` AS p9 ON (p8.p_parent_id = p9.p_id)" .
288                " 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" .
289                " 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"
290            )->execute([
291                'filter_1' => preg_quote($filter),
292                'filter_2' => preg_quote($filter),
293                'tree_id' => $tree->getTreeId(),
294                'collation' => I18N::collation(),
295            ])->fetchOneColumn();
296        foreach ($rows as $row) {
297            $places[] = new self($row, $tree);
298        }
299
300        return $places;
301    }
302}
303