xref: /webtrees/app/Module/PlaceHierarchyListModule.php (revision edfa2139df0c8d53399b8c5ec7d19076af3d6b8a)
167992b6aSRichard Cissee<?php
23976b470SGreg Roach
367992b6aSRichard Cissee/**
467992b6aSRichard Cissee * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
667992b6aSRichard Cissee * This program is free software: you can redistribute it and/or modify
767992b6aSRichard Cissee * it under the terms of the GNU General Public License as published by
867992b6aSRichard Cissee * the Free Software Foundation, either version 3 of the License, or
967992b6aSRichard Cissee * (at your option) any later version.
1067992b6aSRichard Cissee * This program is distributed in the hope that it will be useful,
1167992b6aSRichard Cissee * but WITHOUT ANY WARRANTY; without even the implied warranty of
1267992b6aSRichard Cissee * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1367992b6aSRichard Cissee * GNU General Public License for more details.
1467992b6aSRichard Cissee * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1667992b6aSRichard Cissee */
17fcfa147eSGreg Roach
1867992b6aSRichard Cisseedeclare(strict_types=1);
1967992b6aSRichard Cissee
2067992b6aSRichard Cisseenamespace Fisharebest\Webtrees\Module;
2167992b6aSRichard Cissee
2267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Auth;
23c8db8a43SGreg Roachuse Fisharebest\Webtrees\Family;
2467992b6aSRichard Cisseeuse Fisharebest\Webtrees\I18N;
25c8db8a43SGreg Roachuse Fisharebest\Webtrees\Individual;
26c8db8a43SGreg Roachuse Fisharebest\Webtrees\Location;
274b2f1dbbSGreg Roachuse Fisharebest\Webtrees\Place;
284b2f1dbbSGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
29158900c2SGreg Roachuse Fisharebest\Webtrees\Registry;
30c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Services\LeafletJsService;
314b2f1dbbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
324b2f1dbbSGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
335229eadeSGreg Roachuse Fisharebest\Webtrees\Tree;
34b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
354b2f1dbbSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
36a020b8bdSGreg Roachuse Illuminate\Database\Query\Builder;
37a020b8bdSGreg Roachuse Illuminate\Database\Query\JoinClause;
386ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface;
396ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
404b2f1dbbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
41f3874e19SGreg Roach
424b2f1dbbSGreg Roachuse function array_chunk;
434b2f1dbbSGreg Roachuse function array_pop;
444b2f1dbbSGreg Roachuse function array_reverse;
454b2f1dbbSGreg Roachuse function ceil;
464b2f1dbbSGreg Roachuse function count;
474b2f1dbbSGreg Roachuse function redirect;
484b2f1dbbSGreg Roachuse function route;
494b2f1dbbSGreg Roachuse function view;
5067992b6aSRichard Cissee
5167992b6aSRichard Cissee/**
5267992b6aSRichard Cissee * Class IndividualListModule
5367992b6aSRichard Cissee */
544b2f1dbbSGreg Roachclass PlaceHierarchyListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
5567992b6aSRichard Cissee{
5667992b6aSRichard Cissee    use ModuleListTrait;
5767992b6aSRichard Cissee
584b2f1dbbSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/place-list';
594b2f1dbbSGreg Roach
604b2f1dbbSGreg Roach    /** @var int The default access level for this module.  It can be changed in the control panel. */
6133c746f1SGreg Roach    protected int $access_level = Auth::PRIV_USER;
624b2f1dbbSGreg Roach
63c9c6f2ecSGreg Roach    private LeafletJsService $leaflet_js_service;
64c9c6f2ecSGreg Roach
65c9c6f2ecSGreg Roach    private ModuleService $module_service;
66c9c6f2ecSGreg Roach
67c9c6f2ecSGreg Roach    private SearchService $search_service;
684b2f1dbbSGreg Roach
694b2f1dbbSGreg Roach    /**
704b2f1dbbSGreg Roach     * PlaceHierarchy constructor.
714b2f1dbbSGreg Roach     *
72c9c6f2ecSGreg Roach     * @param LeafletJsService $leaflet_js_service
73c9c6f2ecSGreg Roach     * @param ModuleService    $module_service
744b2f1dbbSGreg Roach     * @param SearchService    $search_service
754b2f1dbbSGreg Roach     */
76c9c6f2ecSGreg Roach    public function __construct(LeafletJsService $leaflet_js_service, ModuleService $module_service, SearchService $search_service)
774b2f1dbbSGreg Roach    {
78c9c6f2ecSGreg Roach        $this->leaflet_js_service = $leaflet_js_service;
79c9c6f2ecSGreg Roach        $this->module_service     = $module_service;
804b2f1dbbSGreg Roach        $this->search_service     = $search_service;
814b2f1dbbSGreg Roach    }
824b2f1dbbSGreg Roach
834b2f1dbbSGreg Roach    /**
844b2f1dbbSGreg Roach     * Initialization.
854b2f1dbbSGreg Roach     *
864b2f1dbbSGreg Roach     * @return void
874b2f1dbbSGreg Roach     */
884b2f1dbbSGreg Roach    public function boot(): void
894b2f1dbbSGreg Roach    {
90158900c2SGreg Roach        Registry::routeFactory()->routeMap()
914b2f1dbbSGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
924b2f1dbbSGreg Roach    }
934b2f1dbbSGreg Roach
9467992b6aSRichard Cissee    /**
950cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
9667992b6aSRichard Cissee     *
9767992b6aSRichard Cissee     * @return string
9867992b6aSRichard Cissee     */
9967992b6aSRichard Cissee    public function title(): string
10067992b6aSRichard Cissee    {
10167992b6aSRichard Cissee        /* I18N: Name of a module/list */
10267992b6aSRichard Cissee        return I18N::translate('Place hierarchy');
10367992b6aSRichard Cissee    }
10467992b6aSRichard Cissee
10567992b6aSRichard Cissee    /**
10667992b6aSRichard Cissee     * A sentence describing what this module does.
10767992b6aSRichard Cissee     *
10867992b6aSRichard Cissee     * @return string
10967992b6aSRichard Cissee     */
11067992b6aSRichard Cissee    public function description(): string
11167992b6aSRichard Cissee    {
1129e0868cbSGreg Roach        /* I18N: Description of the “Place hierarchy” module */
11367992b6aSRichard Cissee        return I18N::translate('The place hierarchy.');
11467992b6aSRichard Cissee    }
11567992b6aSRichard Cissee
11667992b6aSRichard Cissee    /**
11767992b6aSRichard Cissee     * CSS class for the URL.
11867992b6aSRichard Cissee     *
11967992b6aSRichard Cissee     * @return string
12067992b6aSRichard Cissee     */
12167992b6aSRichard Cissee    public function listMenuClass(): string
12267992b6aSRichard Cissee    {
12367992b6aSRichard Cissee        return 'menu-list-plac';
12467992b6aSRichard Cissee    }
12567992b6aSRichard Cissee
1264db4b4a9SGreg Roach    /**
12724f2a3afSGreg Roach     * @return array<string>
1284db4b4a9SGreg Roach     */
12967992b6aSRichard Cissee    public function listUrlAttributes(): array
13067992b6aSRichard Cissee    {
13167992b6aSRichard Cissee        return [];
13267992b6aSRichard Cissee    }
1334b2f1dbbSGreg Roach
1344b2f1dbbSGreg Roach    /**
1354b2f1dbbSGreg Roach     * @param Tree $tree
1364b2f1dbbSGreg Roach     *
1374b2f1dbbSGreg Roach     * @return bool
1384b2f1dbbSGreg Roach     */
1394b2f1dbbSGreg Roach    public function listIsEmpty(Tree $tree): bool
1404b2f1dbbSGreg Roach    {
1414b2f1dbbSGreg Roach        return !DB::table('places')
1424b2f1dbbSGreg Roach            ->where('p_file', '=', $tree->id())
1434b2f1dbbSGreg Roach            ->exists();
1444b2f1dbbSGreg Roach    }
1454b2f1dbbSGreg Roach
1464b2f1dbbSGreg Roach    /**
1474b2f1dbbSGreg Roach     * Handle URLs generated by older versions of webtrees
1484b2f1dbbSGreg Roach     *
1494b2f1dbbSGreg Roach     * @param ServerRequestInterface $request
1504b2f1dbbSGreg Roach     *
1514b2f1dbbSGreg Roach     * @return ResponseInterface
1524b2f1dbbSGreg Roach     */
1534b2f1dbbSGreg Roach    public function getListAction(ServerRequestInterface $request): ResponseInterface
1544b2f1dbbSGreg Roach    {
155b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
156b55cbc6bSGreg Roach
157b55cbc6bSGreg Roach        return redirect($this->listUrl($tree, $request->getQueryParams()));
1584b2f1dbbSGreg Roach    }
1594b2f1dbbSGreg Roach
1604b2f1dbbSGreg Roach    /**
161c9c6f2ecSGreg Roach     * @param Tree                                      $tree
16276d39c55SGreg Roach     * @param array<bool|int|string|array<string>|null> $parameters
163c9c6f2ecSGreg Roach     *
164c9c6f2ecSGreg Roach     * @return string
165c9c6f2ecSGreg Roach     */
166c9c6f2ecSGreg Roach    public function listUrl(Tree $tree, array $parameters = []): string
167c9c6f2ecSGreg Roach    {
168c9c6f2ecSGreg Roach        $parameters['tree'] = $tree->name();
169c9c6f2ecSGreg Roach
170c9c6f2ecSGreg Roach        return route(static::class, $parameters);
171c9c6f2ecSGreg Roach    }
172c9c6f2ecSGreg Roach
173c9c6f2ecSGreg Roach    /**
1744b2f1dbbSGreg Roach     * @param ServerRequestInterface $request
1754b2f1dbbSGreg Roach     *
1764b2f1dbbSGreg Roach     * @return ResponseInterface
1774b2f1dbbSGreg Roach     */
1784b2f1dbbSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
1794b2f1dbbSGreg Roach    {
180b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
181b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
1824b2f1dbbSGreg Roach
1834b2f1dbbSGreg Roach        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
1844b2f1dbbSGreg Roach
1854b2f1dbbSGreg Roach        $action2  = $request->getQueryParams()['action2'] ?? 'hierarchy';
1864b2f1dbbSGreg Roach        $place_id = (int) ($request->getQueryParams()['place_id'] ?? 0);
1874b2f1dbbSGreg Roach        $place    = Place::find($place_id, $tree);
1884b2f1dbbSGreg Roach
1894b2f1dbbSGreg Roach        // Request for a non-existent place?
1904b2f1dbbSGreg Roach        if ($place_id !== $place->id()) {
1914b2f1dbbSGreg Roach            return redirect($place->url());
1924b2f1dbbSGreg Roach        }
1934b2f1dbbSGreg Roach
194c9c6f2ecSGreg Roach        $map_providers = $this->module_service->findByInterface(ModuleMapProviderInterface::class);
195c9c6f2ecSGreg Roach
1964b2f1dbbSGreg Roach        $content = '';
197c9c6f2ecSGreg Roach        $showmap = $map_providers->isNotEmpty();
1984b2f1dbbSGreg Roach        $data    = null;
1994b2f1dbbSGreg Roach
2004b2f1dbbSGreg Roach        if ($showmap) {
2014b2f1dbbSGreg Roach            $content .= view('modules/place-hierarchy/map', [
202*edfa2139SGreg Roach                'data'           => $this->mapData($place),
203c9c6f2ecSGreg Roach                'leaflet_config' => $this->leaflet_js_service->config(),
2044b2f1dbbSGreg Roach            ]);
2054b2f1dbbSGreg Roach        }
2064b2f1dbbSGreg Roach
2074b2f1dbbSGreg Roach        switch ($action2) {
2084b2f1dbbSGreg Roach            case 'list':
2094b2f1dbbSGreg Roach            default:
2104b2f1dbbSGreg Roach                $alt_link = I18N::translate('Show place hierarchy');
2114b2f1dbbSGreg Roach                $alt_url  = $this->listUrl($tree, ['action2' => 'hierarchy', 'place_id' => $place_id]);
2124b2f1dbbSGreg Roach                $content .= view('modules/place-hierarchy/list', ['columns' => $this->getList($tree)]);
2134b2f1dbbSGreg Roach                break;
2144b2f1dbbSGreg Roach            case 'hierarchy':
2154b2f1dbbSGreg Roach            case 'hierarchy-e':
2164b2f1dbbSGreg Roach                $alt_link = I18N::translate('Show all places in a list');
2174b2f1dbbSGreg Roach                $alt_url  = $this->listUrl($tree, ['action2' => 'list', 'place_id' => 0]);
2184b2f1dbbSGreg Roach                $data     = $this->getHierarchy($place);
21961999471SGreg Roach                $content .= ($data === null || $showmap) ? '' : view('place-hierarchy', $data);
22061999471SGreg Roach                if ($data === null || $action2 === 'hierarchy-e') {
2214b2f1dbbSGreg Roach                    $content .= view('modules/place-hierarchy/events', [
2224b2f1dbbSGreg Roach                        'indilist' => $this->search_service->searchIndividualsInPlace($place),
2234b2f1dbbSGreg Roach                        'famlist'  => $this->search_service->searchFamiliesInPlace($place),
2244b2f1dbbSGreg Roach                        'tree'     => $place->tree(),
2254b2f1dbbSGreg Roach                    ]);
2264b2f1dbbSGreg Roach                }
2274b2f1dbbSGreg Roach        }
2284b2f1dbbSGreg Roach
2294b2f1dbbSGreg Roach        if ($data !== null && $action2 !== 'hierarchy-e' && $place->gedcomName() !== '') {
2304b2f1dbbSGreg Roach            $events_link = $this->listUrl($tree, ['action2' => 'hierarchy-e', 'place_id' => $place_id]);
2314b2f1dbbSGreg Roach        } else {
2324b2f1dbbSGreg Roach            $events_link = '';
2334b2f1dbbSGreg Roach        }
2344b2f1dbbSGreg Roach
2354b2f1dbbSGreg Roach        $breadcrumbs = $this->breadcrumbs($place);
2364b2f1dbbSGreg Roach
2374b2f1dbbSGreg Roach        return $this->viewResponse('modules/place-hierarchy/page', [
2384b2f1dbbSGreg Roach            'alt_link'    => $alt_link,
2394b2f1dbbSGreg Roach            'alt_url'     => $alt_url,
2404b2f1dbbSGreg Roach            'breadcrumbs' => $breadcrumbs['breadcrumbs'],
2414b2f1dbbSGreg Roach            'content'     => $content,
2424b2f1dbbSGreg Roach            'current'     => $breadcrumbs['current'],
2434b2f1dbbSGreg Roach            'events_link' => $events_link,
2444b2f1dbbSGreg Roach            'place'       => $place,
2454b2f1dbbSGreg Roach            'title'       => I18N::translate('Place hierarchy'),
2464b2f1dbbSGreg Roach            'tree'        => $tree,
247c9c6f2ecSGreg Roach            'world_url'   => $this->listUrl($tree),
2484b2f1dbbSGreg Roach        ]);
2494b2f1dbbSGreg Roach    }
2504b2f1dbbSGreg Roach
2514b2f1dbbSGreg Roach    /**
2524b2f1dbbSGreg Roach     * @param Place $placeObj
2534b2f1dbbSGreg Roach     *
2544b2f1dbbSGreg Roach     * @return array<mixed>
2554b2f1dbbSGreg Roach     */
256*edfa2139SGreg Roach    protected function mapData(Place $placeObj): array
2574b2f1dbbSGreg Roach    {
2584b2f1dbbSGreg Roach        $places    = $placeObj->getChildPlaces();
2594b2f1dbbSGreg Roach        $features  = [];
2604b2f1dbbSGreg Roach        $sidebar   = '';
2614b2f1dbbSGreg Roach        $show_link = true;
2624b2f1dbbSGreg Roach
2634b2f1dbbSGreg Roach        if ($places === []) {
2644b2f1dbbSGreg Roach            $places[]  = $placeObj;
2654b2f1dbbSGreg Roach            $show_link = false;
2664b2f1dbbSGreg Roach        }
2674b2f1dbbSGreg Roach
2684b2f1dbbSGreg Roach        foreach ($places as $id => $place) {
2694b2f1dbbSGreg Roach            $location = new PlaceLocation($place->gedcomName());
2704b2f1dbbSGreg Roach
27190949315SGreg Roach            if ($location->latitude() === null || $location->longitude() === null) {
2724b2f1dbbSGreg Roach                $sidebar_class = 'unmapped';
2734b2f1dbbSGreg Roach            } else {
2744b2f1dbbSGreg Roach                $sidebar_class = 'mapped';
2754b2f1dbbSGreg Roach                $features[]    = [
2764b2f1dbbSGreg Roach                    'type'       => 'Feature',
2774b2f1dbbSGreg Roach                    'id'         => $id,
2784b2f1dbbSGreg Roach                    'geometry'   => [
2794b2f1dbbSGreg Roach                        'type'        => 'Point',
2804b2f1dbbSGreg Roach                        'coordinates' => [$location->longitude(), $location->latitude()],
2814b2f1dbbSGreg Roach                    ],
2824b2f1dbbSGreg Roach                    'properties' => [
2834b2f1dbbSGreg Roach                        'tooltip' => $place->gedcomName(),
2844b2f1dbbSGreg Roach                        'popup'   => view('modules/place-hierarchy/popup', [
2854b2f1dbbSGreg Roach                            'showlink'  => $show_link,
2864b2f1dbbSGreg Roach                            'place'     => $place,
2874b2f1dbbSGreg Roach                            'latitude'  => $location->latitude(),
2884b2f1dbbSGreg Roach                            'longitude' => $location->longitude(),
2894b2f1dbbSGreg Roach                        ]),
2904b2f1dbbSGreg Roach                    ],
2914b2f1dbbSGreg Roach                ];
2924b2f1dbbSGreg Roach            }
2934b2f1dbbSGreg Roach
294a020b8bdSGreg Roach            $stats = [
295a020b8bdSGreg Roach                Family::RECORD_TYPE     => $this->familyPlaceLinks($place)->count(),
296a020b8bdSGreg Roach                Individual::RECORD_TYPE => $this->individualPlaceLinks($place)->count(),
297a020b8bdSGreg Roach                Location::RECORD_TYPE   => $this->locationPlaceLinks($place)->count(),
298a020b8bdSGreg Roach            ];
2994b2f1dbbSGreg Roach
3004b2f1dbbSGreg Roach            $sidebar .= view('modules/place-hierarchy/sidebar', [
3014b2f1dbbSGreg Roach                'showlink'      => $show_link,
3024b2f1dbbSGreg Roach                'id'            => $id,
3034b2f1dbbSGreg Roach                'place'         => $place,
3044b2f1dbbSGreg Roach                'sidebar_class' => $sidebar_class,
305c8db8a43SGreg Roach                'stats'         => $stats,
3064b2f1dbbSGreg Roach            ]);
3074b2f1dbbSGreg Roach        }
3084b2f1dbbSGreg Roach
3094b2f1dbbSGreg Roach        return [
3104b2f1dbbSGreg Roach            'bounds'  => (new PlaceLocation($placeObj->gedcomName()))->boundingRectangle(),
3114b2f1dbbSGreg Roach            'sidebar' => $sidebar,
3124b2f1dbbSGreg Roach            'markers' => [
3134b2f1dbbSGreg Roach                'type'     => 'FeatureCollection',
3144b2f1dbbSGreg Roach                'features' => $features,
315c9c6f2ecSGreg Roach            ],
316c9c6f2ecSGreg Roach        ];
317c9c6f2ecSGreg Roach    }
318c9c6f2ecSGreg Roach
319c9c6f2ecSGreg Roach    /**
320c9c6f2ecSGreg Roach     * @param Tree $tree
321c9c6f2ecSGreg Roach     *
322c9c6f2ecSGreg Roach     * @return array<array<Place>>
323c9c6f2ecSGreg Roach     */
324c9c6f2ecSGreg Roach    private function getList(Tree $tree): array
325c9c6f2ecSGreg Roach    {
326c9c6f2ecSGreg Roach        $places = $this->search_service->searchPlaces($tree, '')
327c9c6f2ecSGreg Roach            ->sort(static function (Place $x, Place $y): int {
328c9c6f2ecSGreg Roach                return $x->gedcomName() <=> $y->gedcomName();
329c9c6f2ecSGreg Roach            })
330c9c6f2ecSGreg Roach            ->all();
331c9c6f2ecSGreg Roach
332c9c6f2ecSGreg Roach        $count = count($places);
333c9c6f2ecSGreg Roach
334c9c6f2ecSGreg Roach        if ($places === []) {
335c9c6f2ecSGreg Roach            return [];
336c9c6f2ecSGreg Roach        }
337c9c6f2ecSGreg Roach
338c9c6f2ecSGreg Roach        $columns = $count > 20 ? 3 : 2;
339c9c6f2ecSGreg Roach
340c9c6f2ecSGreg Roach        return array_chunk($places, (int) ceil($count / $columns));
341c9c6f2ecSGreg Roach    }
342c9c6f2ecSGreg Roach
343c9c6f2ecSGreg Roach    /**
344c9c6f2ecSGreg Roach     * @param Place $place
345c9c6f2ecSGreg Roach     *
346c9c6f2ecSGreg Roach     * @return array{'tree':Tree,'col_class':string,'columns':array<array<Place>>,'place':Place}|null
347c9c6f2ecSGreg Roach     */
348c9c6f2ecSGreg Roach    private function getHierarchy(Place $place): ?array
349c9c6f2ecSGreg Roach    {
350c9c6f2ecSGreg Roach        $child_places = $place->getChildPlaces();
351c9c6f2ecSGreg Roach        $numfound     = count($child_places);
352c9c6f2ecSGreg Roach
353c9c6f2ecSGreg Roach        if ($numfound > 0) {
354c9c6f2ecSGreg Roach            $divisor = $numfound > 20 ? 3 : 2;
355c9c6f2ecSGreg Roach
356c9c6f2ecSGreg Roach            return [
357c9c6f2ecSGreg Roach                'tree'      => $place->tree(),
358c9c6f2ecSGreg Roach                'col_class' => 'w-' . ($divisor === 2 ? '25' : '50'),
359c9c6f2ecSGreg Roach                'columns'   => array_chunk($child_places, (int) ceil($numfound / $divisor)),
360c9c6f2ecSGreg Roach                'place'     => $place,
361c9c6f2ecSGreg Roach            ];
362c9c6f2ecSGreg Roach        }
363c9c6f2ecSGreg Roach
364c9c6f2ecSGreg Roach        return null;
365c9c6f2ecSGreg Roach    }
366c9c6f2ecSGreg Roach
367c9c6f2ecSGreg Roach    /**
368c9c6f2ecSGreg Roach     * @param Place $place
369c9c6f2ecSGreg Roach     *
370c9c6f2ecSGreg Roach     * @return array{'breadcrumbs':array<Place>,'current':Place|null}
371c9c6f2ecSGreg Roach     */
372c9c6f2ecSGreg Roach    private function breadcrumbs(Place $place): array
373c9c6f2ecSGreg Roach    {
374c9c6f2ecSGreg Roach        $breadcrumbs = [];
375c9c6f2ecSGreg Roach        if ($place->gedcomName() !== '') {
376c9c6f2ecSGreg Roach            $breadcrumbs[] = $place;
377c9c6f2ecSGreg Roach            $parent_place  = $place->parent();
378c9c6f2ecSGreg Roach            while ($parent_place->gedcomName() !== '') {
379c9c6f2ecSGreg Roach                $breadcrumbs[] = $parent_place;
380c9c6f2ecSGreg Roach                $parent_place  = $parent_place->parent();
381c9c6f2ecSGreg Roach            }
382c9c6f2ecSGreg Roach            $breadcrumbs = array_reverse($breadcrumbs);
383c9c6f2ecSGreg Roach            $current     = array_pop($breadcrumbs);
384c9c6f2ecSGreg Roach        } else {
385c9c6f2ecSGreg Roach            $current = null;
386c9c6f2ecSGreg Roach        }
387c9c6f2ecSGreg Roach
388c9c6f2ecSGreg Roach        return [
389c9c6f2ecSGreg Roach            'breadcrumbs' => $breadcrumbs,
390c9c6f2ecSGreg Roach            'current'     => $current,
3914b2f1dbbSGreg Roach        ];
3924b2f1dbbSGreg Roach    }
393a020b8bdSGreg Roach
394a020b8bdSGreg Roach    /**
395a020b8bdSGreg Roach     * @param Place $place
396a020b8bdSGreg Roach     *
397a020b8bdSGreg Roach     * @return Builder
398a020b8bdSGreg Roach     */
399a020b8bdSGreg Roach    private function placeLinks(Place $place): Builder
400a020b8bdSGreg Roach    {
401a020b8bdSGreg Roach        return DB::table('places')
402a020b8bdSGreg Roach            ->join('placelinks', static function (JoinClause $join): void {
403a020b8bdSGreg Roach                $join
404a020b8bdSGreg Roach                    ->on('pl_file', '=', 'p_file')
405a020b8bdSGreg Roach                    ->on('pl_p_id', '=', 'p_id');
406a020b8bdSGreg Roach            })
407a020b8bdSGreg Roach            ->where('p_file', '=', $place->tree()->id())
408a020b8bdSGreg Roach            ->where('p_id', '=', $place->id());
409a020b8bdSGreg Roach    }
410a020b8bdSGreg Roach
411a020b8bdSGreg Roach    /**
412a020b8bdSGreg Roach     * @param Place $place
413a020b8bdSGreg Roach     *
414a020b8bdSGreg Roach     * @return Builder
415a020b8bdSGreg Roach     */
416a020b8bdSGreg Roach    private function familyPlaceLinks(Place $place): Builder
417a020b8bdSGreg Roach    {
418a020b8bdSGreg Roach        return $this->placeLinks($place)
419a020b8bdSGreg Roach            ->join('families', static function (JoinClause $join): void {
420a020b8bdSGreg Roach                $join
421a020b8bdSGreg Roach                    ->on('pl_file', '=', 'f_file')
422a020b8bdSGreg Roach                    ->on('pl_gid', '=', 'f_id');
423a020b8bdSGreg Roach            });
424a020b8bdSGreg Roach    }
425a020b8bdSGreg Roach
426a020b8bdSGreg Roach    /**
427a020b8bdSGreg Roach     * @param Place $place
428a020b8bdSGreg Roach     *
429a020b8bdSGreg Roach     * @return Builder
430a020b8bdSGreg Roach     */
431a020b8bdSGreg Roach    private function individualPlaceLinks(Place $place): Builder
432a020b8bdSGreg Roach    {
433a020b8bdSGreg Roach        return $this->placeLinks($place)
434a020b8bdSGreg Roach            ->join('individuals', static function (JoinClause $join): void {
435a020b8bdSGreg Roach                $join
436a020b8bdSGreg Roach                    ->on('pl_file', '=', 'i_file')
437a020b8bdSGreg Roach                    ->on('pl_gid', '=', 'i_id');
438a020b8bdSGreg Roach            });
439a020b8bdSGreg Roach    }
440a020b8bdSGreg Roach
441a020b8bdSGreg Roach    /**
442a020b8bdSGreg Roach     * @param Place $place
443a020b8bdSGreg Roach     *
444a020b8bdSGreg Roach     * @return Builder
445a020b8bdSGreg Roach     */
446a020b8bdSGreg Roach    private function locationPlaceLinks(Place $place): Builder
447a020b8bdSGreg Roach    {
448a020b8bdSGreg Roach        return $this->placeLinks($place)
449a020b8bdSGreg Roach            ->join('other', static function (JoinClause $join): void {
450a020b8bdSGreg Roach                $join
451a020b8bdSGreg Roach                    ->on('pl_file', '=', 'o_file')
452a020b8bdSGreg Roach                    ->on('pl_gid', '=', 'o_id');
453a020b8bdSGreg Roach            })
454a020b8bdSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE);
455a020b8bdSGreg Roach    }
45667992b6aSRichard Cissee}
457