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
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2019 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/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Support\Collection;
/**
* A GEDCOM place (PLAC) object.
*/
class Place
{
/** @var string e.g. "Westminster, London, England" */
private $place_name;
/** @var Collection|string[] The parts of a place name, e.g. ["Westminster", "London", "England"] */
private $parts;
/** @var Tree We may have the same place name in different trees. */
private $tree;
/**
* Create a place.
*
* @param string $place_name
* @param Tree $tree
*/
public function __construct(string $place_name, Tree $tree)
{
// Ignore any empty parts in place names such as "Village, , , Country".
$this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $place_name)))
->filter();
// Rebuild the placename in the correct format.
$this->place_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
$this->tree = $tree;
}
/**
* Get the higher level place.
*
* @return Place
*/
public function parent(): Place
{
return new static($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR), $this->tree);
}
/**
* The database row that contains this place.
* Note that due to database collation, both "Quebec" and "Québec" will share the same row.
*
* @return int
*/
public function id(): int
{
return app()->make('cache.array')->rememberForever('place:' . $this->place_name, function () {
// The "top-level" place won't exist in the database.
if ($this->parts->isEmpty()) {
return 0;
}
$parent_place = $this->parent();
$place_id = (int) DB::table('places')
->where('p_file', '=', $this->tree->id())
->where('p_place', '=', $this->parts->first())
->where('p_parent_id', '=', $parent_place->id())
->value('p_id');
if ($place_id === 0) {
$place = $this->parts->first();
DB::table('places')->insert([
'p_file' => $this->tree->id(),
'p_place' => $place,
'p_parent_id' => $parent_place->id(),
'p_std_soundex' => Soundex::russell($place),
'p_dm_soundex' => Soundex::daitchMokotoff($place),
]);
$place_id = (int) DB::connection()->getPdo()->lastInsertId();
}
return $place_id;
});
}
/**
* Extract the locality (first parts) of a place name.
*
* @param int $n
*
* @return Collection
*/
public function firstParts(int $n): Collection
{
return $this->parts->slice(0, $n);
}
/**
* Extract the country (last parts) of a place name.
*
* @param int $n
*
* @return Collection
*/
public function lastParts(int $n): Collection
{
return $this->parts->slice(-$n);
}
/**
* Get the lower level places.
*
* @return Place[]
*/
public function getChildPlaces(): array
{
if ($this->place_name !== '') {
$parent_text = Gedcom::PLACE_SEPARATOR . $this->place_name;
} else {
$parent_text = '';
}
return DB::table('places')
->where('p_file', '=', $this->tree->id())
->where('p_parent_id', '=', $this->id())
->orderBy(DB::raw('p_place /*! COLLATE ' . I18N::collation() . ' */'))
->pluck('p_place')
->map(function (string $place) use ($parent_text): Place {
return new self($place . $parent_text, $this->tree);
})
->all();
}
/**
* Create a URL to the place-hierarchy page.
*
* @return string
*/
public function url(): string
{
return route('place-hierarchy', [
'parent' => $this->parts->reverse()->all(),
'ged' => $this->tree->name(),
]);
}
/**
* Format this place for GEDCOM data.
*
* @return string
*/
public function gedcomName(): string
{
return $this->place_name;
}
/**
* Format this place for display on screen.
*
* @return string
*/
public function placeName(): string
{
$place_name = $this->parts->first() ?? I18N::translate('unknown');
return '<span dir="auto">' . e($place_name) . '</span>';
}
/**
* Generate the place name for display, including the full hierarchy.
*
* @param bool $link
*
* @return string
*/
public function fullName(bool $link = false)
{
if ($this->parts->isEmpty()) {
return '';
}
$full_name = $this->parts->implode(I18N::$list_separator);
if ($link) {
return '<a dir="auto" href="' . e($this->url()) . '">' . e($full_name) . '</a>';
}
return '<span dir="auto">' . e($full_name) . '</span>';
}
/**
* For lists and charts, where the full name won’t fit.
*
* @param bool $link
*
* @return string
*/
public function shortName(bool $link = false)
{
$SHOW_PEDIGREE_PLACES = (int) $this->tree->getPreference('SHOW_PEDIGREE_PLACES');
// Abbreviate the place name, for lists
if ($this->tree->getPreference('SHOW_PEDIGREE_PLACES_SUFFIX')) {
$parts = $this->lastParts($SHOW_PEDIGREE_PLACES);
} else {
$parts = $this->firstParts($SHOW_PEDIGREE_PLACES);
}
$short_name = $parts->implode(I18N::$list_separator);
// Add a tool-tip showing the full name
$title = strip_tags($this->fullName());
if ($link) {
return '<a dir="auto" href="' . e($this->url()) . '" title="' . $title . '"">' . e($short_name) . '</a>';
}
return '<span dir="auto">' . e($short_name) . '</span>';
}
}
|