1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2022 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Module; |
21
|
|
|
|
22
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
23
|
|
|
use Fisharebest\Webtrees\Auth; |
24
|
|
|
use Fisharebest\Webtrees\Family; |
25
|
|
|
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataEdit; |
26
|
|
|
use Fisharebest\Webtrees\I18N; |
27
|
|
|
use Fisharebest\Webtrees\Individual; |
28
|
|
|
use Fisharebest\Webtrees\Location; |
29
|
|
|
use Fisharebest\Webtrees\Place; |
30
|
|
|
use Fisharebest\Webtrees\PlaceLocation; |
31
|
|
|
use Fisharebest\Webtrees\Registry; |
32
|
|
|
use Fisharebest\Webtrees\Services\LeafletJsService; |
33
|
|
|
use Fisharebest\Webtrees\Services\ModuleService; |
34
|
|
|
use Fisharebest\Webtrees\Services\SearchService; |
35
|
|
|
use Fisharebest\Webtrees\Tree; |
36
|
|
|
use Fisharebest\Webtrees\Validator; |
37
|
|
|
use Illuminate\Database\Capsule\Manager as DB; |
38
|
|
|
use Illuminate\Database\Query\Builder; |
39
|
|
|
use Illuminate\Database\Query\JoinClause; |
40
|
|
|
use Psr\Http\Message\ResponseInterface; |
41
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
42
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
43
|
|
|
|
44
|
|
|
use function array_chunk; |
45
|
|
|
use function array_pop; |
46
|
|
|
use function array_reverse; |
47
|
|
|
use function ceil; |
48
|
|
|
use function count; |
49
|
|
|
use function redirect; |
50
|
|
|
use function route; |
51
|
|
|
use function view; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Class IndividualListModule |
55
|
|
|
*/ |
56
|
|
|
class PlaceHierarchyListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface |
57
|
|
|
{ |
58
|
|
|
use ModuleListTrait; |
59
|
|
|
|
60
|
|
|
protected const ROUTE_URL = '/tree/{tree}/place-list'; |
61
|
|
|
|
62
|
|
|
/** @var int The default access level for this module. It can be changed in the control panel. */ |
63
|
|
|
protected int $access_level = Auth::PRIV_USER; |
64
|
|
|
|
65
|
|
|
private LeafletJsService $leaflet_js_service; |
66
|
|
|
|
67
|
|
|
private ModuleService $module_service; |
68
|
|
|
|
69
|
|
|
private SearchService $search_service; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* PlaceHierarchy constructor. |
73
|
|
|
* |
74
|
|
|
* @param LeafletJsService $leaflet_js_service |
75
|
|
|
* @param ModuleService $module_service |
76
|
|
|
* @param SearchService $search_service |
77
|
|
|
*/ |
78
|
|
|
public function __construct(LeafletJsService $leaflet_js_service, ModuleService $module_service, SearchService $search_service) |
79
|
|
|
{ |
80
|
|
|
$this->leaflet_js_service = $leaflet_js_service; |
81
|
|
|
$this->module_service = $module_service; |
82
|
|
|
$this->search_service = $search_service; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initialization. |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function boot(): void |
91
|
|
|
{ |
92
|
|
|
Registry::routeFactory()->routeMap() |
93
|
|
|
->get(static::class, static::ROUTE_URL, $this); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* How should this module be identified in the control panel, etc.? |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function title(): string |
102
|
|
|
{ |
103
|
|
|
/* I18N: Name of a module/list */ |
104
|
|
|
return I18N::translate('Place hierarchy'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* A sentence describing what this module does. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function description(): string |
113
|
|
|
{ |
114
|
|
|
/* I18N: Description of the “Place hierarchy” module */ |
115
|
|
|
return I18N::translate('The place hierarchy.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* CSS class for the URL. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function listMenuClass(): string |
124
|
|
|
{ |
125
|
|
|
return 'menu-list-plac'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array<string> |
130
|
|
|
*/ |
131
|
|
|
public function listUrlAttributes(): array |
132
|
|
|
{ |
133
|
|
|
return []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Tree $tree |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function listIsEmpty(Tree $tree): bool |
142
|
|
|
{ |
143
|
|
|
return !DB::table('places') |
144
|
|
|
->where('p_file', '=', $tree->id()) |
145
|
|
|
->exists(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Handle URLs generated by older versions of webtrees |
150
|
|
|
* |
151
|
|
|
* @param ServerRequestInterface $request |
152
|
|
|
* |
153
|
|
|
* @return ResponseInterface |
154
|
|
|
*/ |
155
|
|
|
public function getListAction(ServerRequestInterface $request): ResponseInterface |
156
|
|
|
{ |
157
|
|
|
$tree = Validator::attributes($request)->tree(); |
158
|
|
|
|
159
|
|
|
return redirect($this->listUrl($tree, $request->getQueryParams())); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param Tree $tree |
164
|
|
|
* @param array<bool|int|string|array<string>|null> $parameters |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function listUrl(Tree $tree, array $parameters = []): string |
169
|
|
|
{ |
170
|
|
|
$parameters['tree'] = $tree->name(); |
171
|
|
|
|
172
|
|
|
return route(static::class, $parameters); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param ServerRequestInterface $request |
177
|
|
|
* |
178
|
|
|
* @return ResponseInterface |
179
|
|
|
*/ |
180
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
181
|
|
|
{ |
182
|
|
|
$tree = Validator::attributes($request)->tree(); |
183
|
|
|
$user = Validator::attributes($request)->user(); |
184
|
|
|
|
185
|
|
|
Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); |
186
|
|
|
|
187
|
|
|
$place_id = Validator::queryParams($request)->integer('place_id', 0); |
188
|
|
|
$map_providers = $this->module_service->findByInterface(ModuleMapProviderInterface::class); |
189
|
|
|
$use_map = $map_providers->isNotEmpty(); |
190
|
|
|
$content = ''; |
191
|
|
|
|
192
|
|
|
if ($use_map) { |
193
|
|
|
$content = view('modules/place-hierarchy/map', [ |
194
|
|
|
'leaflet_config' => $this->leaflet_js_service->config(), |
195
|
|
|
]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->viewResponse('modules/place-hierarchy/page', [ |
199
|
|
|
'content' => $content, |
200
|
|
|
'title' => I18N::translate('Place hierarchy'), |
201
|
|
|
'tree' => $tree, |
202
|
|
|
'use_map' => $use_map, |
203
|
|
|
'place_summary' => ['id' => $place_id, 'link' => ''], |
204
|
|
|
]); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Place $placeObj |
209
|
|
|
* |
210
|
|
|
* @return array<mixed> |
211
|
|
|
*/ |
212
|
|
|
protected function mapData(Place $placeObj): array |
213
|
|
|
{ |
214
|
|
|
$places = $placeObj->getChildPlaces(); |
215
|
|
|
$features = []; |
216
|
|
|
$sidebar = ''; |
217
|
|
|
|
218
|
|
|
if ($places === []) { |
219
|
|
|
$places[] = $placeObj; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
foreach ($places as $id => $place) { |
223
|
|
|
$location = new PlaceLocation($place->gedcomName()); |
224
|
|
|
|
225
|
|
|
if ($location->latitude() === null || $location->longitude() === null) { |
226
|
|
|
$sidebar_class = 'unmapped'; |
227
|
|
|
} else { |
228
|
|
|
$sidebar_class = 'mapped'; |
229
|
|
|
$features[] = [ |
230
|
|
|
'type' => 'Feature', |
231
|
|
|
'id' => $id, |
232
|
|
|
'geometry' => [ |
233
|
|
|
'type' => 'Point', |
234
|
|
|
'coordinates' => [$location->longitude(), $location->latitude()], |
235
|
|
|
], |
236
|
|
|
'properties' => [ |
237
|
|
|
'tooltip' => $place->gedcomName(), |
238
|
|
|
'popup' => view('modules/place-hierarchy/popup', [ |
239
|
|
|
'place' => $place, |
240
|
|
|
'latitude' => $location->latitude(), |
241
|
|
|
'longitude' => $location->longitude(), |
242
|
|
|
]), |
243
|
|
|
], |
244
|
|
|
]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$stats = [ |
248
|
|
|
Family::RECORD_TYPE => $this->familyPlaceLinks($place)->count(), |
249
|
|
|
Individual::RECORD_TYPE => $this->individualPlaceLinks($place)->count(), |
250
|
|
|
Location::RECORD_TYPE => $this->locationPlaceLinks($place)->count(), |
251
|
|
|
]; |
252
|
|
|
|
253
|
|
|
$sidebar .= view('modules/place-hierarchy/sidebar', [ |
254
|
|
|
'num_children' => count($place->getChildPlaces()), |
255
|
|
|
'id' => $id, |
256
|
|
|
'place' => $place, |
257
|
|
|
'sidebar_class' => $sidebar_class, |
258
|
|
|
'stats' => $stats, |
259
|
|
|
'geo_link' => Auth::isAdmin() ? route(MapDataEdit::class, ['place_id' => $location->id()]) : '', |
260
|
|
|
]); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return [ |
264
|
|
|
'sidebar' => $sidebar, |
265
|
|
|
'markers' => [ |
266
|
|
|
'type' => 'FeatureCollection', |
267
|
|
|
'features' => $features, |
268
|
|
|
], |
269
|
|
|
]; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param Place $place |
274
|
|
|
* |
275
|
|
|
* @return array<mixed> |
276
|
|
|
*/ |
277
|
|
|
private function getHierarchy(Place $place): array |
278
|
|
|
{ |
279
|
|
|
$places = $place->getChildPlaces(); |
280
|
|
|
$numfound = count($places); |
281
|
|
|
$divisor = $numfound > 20 ? 3 : 2; |
282
|
|
|
|
283
|
|
|
if ($numfound > 0) { |
284
|
|
|
$columns = ceil($numfound / $divisor); |
285
|
|
|
} else { |
286
|
|
|
$columns = 1; |
287
|
|
|
$places = [$place]; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return [ |
291
|
|
|
'col_class' => 'w-' . ($divisor > 2 ? '50' : '25'), |
292
|
|
|
'columns' => array_chunk($places, (int) $columns), |
293
|
|
|
]; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param Place $place |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
private function breadcrumbs(Place $place): string |
302
|
|
|
{ |
303
|
|
|
$place_hierarchy = []; |
304
|
|
|
if ($place->gedcomName() !== '') { |
305
|
|
|
$place_hierarchy[] = $place; |
306
|
|
|
$parent_place = $place->parent(); |
307
|
|
|
while ($parent_place->gedcomName() !== '') { |
308
|
|
|
$place_hierarchy[] = $parent_place; |
309
|
|
|
$parent_place = $parent_place->parent(); |
310
|
|
|
} |
311
|
|
|
$place_hierarchy = array_reverse($place_hierarchy); |
312
|
|
|
$current = array_pop($place_hierarchy); |
313
|
|
|
} else { |
314
|
|
|
$current = null; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return view('modules/place-hierarchy/breadcrumbs', [ |
318
|
|
|
'place_hierarchy' => $place_hierarchy, |
319
|
|
|
'current' => $current, |
320
|
|
|
]); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param ServerRequestInterface $request |
325
|
|
|
* |
326
|
|
|
* @return ResponseInterface |
327
|
|
|
*/ |
328
|
|
|
public function postUpdateDataAction(ServerRequestInterface $request): ResponseInterface |
329
|
|
|
{ |
330
|
|
|
$tree = Validator::attributes($request)->tree(); |
331
|
|
|
$place_id = Validator::parsedBody($request)->integer('placeId', 0); |
332
|
|
|
$type = Validator::parsedBody($request)->string('type', ''); |
333
|
|
|
$place = Place::find($place_id, $tree); |
334
|
|
|
|
335
|
|
|
// Request for a non-existent place? |
336
|
|
|
if ($place_id > 0 && $place->gedcomName() === '') { |
337
|
|
|
return response(I18N::translate('ERROR: the selected place cannot be found'), StatusCodeInterface::STATUS_NOT_FOUND); |
338
|
|
|
} |
339
|
|
|
switch ($type) { |
340
|
|
|
case 'map': |
341
|
|
|
$data = $this->mapData($place); |
342
|
|
|
break; |
343
|
|
|
case 'events': |
344
|
|
|
$data = view('modules/place-hierarchy/events', [ |
345
|
|
|
'indilist' => $this->search_service->searchIndividualsInPlace($place), |
346
|
|
|
'famlist' => $this->search_service->searchFamiliesInPlace($place), |
347
|
|
|
'tree' => $tree, |
348
|
|
|
]); |
349
|
|
|
break; |
350
|
|
|
default: // hierarchy |
351
|
|
|
$data = view('modules/place-hierarchy/hierarchy', $this->getHierarchy($place)); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return response([ |
355
|
|
|
'data' => $data, |
356
|
|
|
'breadcrumbs' => $this->breadcrumbs($place), |
357
|
|
|
'place_summary' => [ |
358
|
|
|
'id' => $place->id(), |
359
|
|
|
'link' => I18N::translate('View table of events occurring in %s', $place->fullName()), |
360
|
|
|
], |
361
|
|
|
]); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param Place $place |
366
|
|
|
* |
367
|
|
|
* @return Builder |
368
|
|
|
*/ |
369
|
|
|
private function placeLinks(Place $place): Builder |
370
|
|
|
{ |
371
|
|
|
return DB::table('places') |
372
|
|
|
->join('placelinks', static function (JoinClause $join): void { |
373
|
|
|
$join |
374
|
|
|
->on('pl_file', '=', 'p_file') |
375
|
|
|
->on('pl_p_id', '=', 'p_id'); |
376
|
|
|
}) |
377
|
|
|
->where('p_file', '=', $place->tree()->id()) |
378
|
|
|
->where('p_id', '=', $place->id()); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param Place $place |
383
|
|
|
* |
384
|
|
|
* @return Builder |
385
|
|
|
*/ |
386
|
|
|
private function familyPlaceLinks(Place $place): Builder |
387
|
|
|
{ |
388
|
|
|
return $this->placeLinks($place) |
389
|
|
|
->join('families', static function (JoinClause $join): void { |
390
|
|
|
$join |
391
|
|
|
->on('pl_file', '=', 'f_file') |
392
|
|
|
->on('pl_gid', '=', 'f_id'); |
393
|
|
|
}); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @param Place $place |
398
|
|
|
* |
399
|
|
|
* @return Builder |
400
|
|
|
*/ |
401
|
|
|
private function individualPlaceLinks(Place $place): Builder |
402
|
|
|
{ |
403
|
|
|
return $this->placeLinks($place) |
404
|
|
|
->join('individuals', static function (JoinClause $join): void { |
405
|
|
|
$join |
406
|
|
|
->on('pl_file', '=', 'i_file') |
407
|
|
|
->on('pl_gid', '=', 'i_id'); |
408
|
|
|
}); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param Place $place |
413
|
|
|
* |
414
|
|
|
* @return Builder |
415
|
|
|
*/ |
416
|
|
|
private function locationPlaceLinks(Place $place): Builder |
417
|
|
|
{ |
418
|
|
|
return $this->placeLinks($place) |
419
|
|
|
->join('other', static function (JoinClause $join): void { |
420
|
|
|
$join |
421
|
|
|
->on('pl_file', '=', 'o_file') |
422
|
|
|
->on('pl_gid', '=', 'o_id'); |
423
|
|
|
}) |
424
|
|
|
->where('o_type', '=', Location::RECORD_TYPE); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|