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