xref: /webtrees/app/Http/RequestHandlers/LocationPage.php (revision 4991f2057a6647447a648c5d6743dab00378e98e)
1e8ded2caSGreg Roach<?php
2e8ded2caSGreg Roach
3e8ded2caSGreg Roach/**
4e8ded2caSGreg Roach * webtrees: online genealogy
5e8ded2caSGreg Roach * Copyright (C) 2021 webtrees development team
6e8ded2caSGreg Roach * This program is free software: you can redistribute it and/or modify
7e8ded2caSGreg Roach * it under the terms of the GNU General Public License as published by
8e8ded2caSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9e8ded2caSGreg Roach * (at your option) any later version.
10e8ded2caSGreg Roach * This program is distributed in the hope that it will be useful,
11e8ded2caSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e8ded2caSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e8ded2caSGreg Roach * GNU General Public License for more details.
14e8ded2caSGreg Roach * You should have received a copy of the GNU General Public License
15e8ded2caSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e8ded2caSGreg Roach */
17e8ded2caSGreg Roach
18e8ded2caSGreg Roachdeclare(strict_types=1);
19e8ded2caSGreg Roach
20e8ded2caSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21e8ded2caSGreg Roach
22e8ded2caSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23e8ded2caSGreg Roachuse Fisharebest\Webtrees\Auth;
24e8ded2caSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
25e8ded2caSGreg Roachuse Fisharebest\Webtrees\Registry;
26*4991f205SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
27*4991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29e8ded2caSGreg Roachuse Illuminate\Support\Collection;
30e8ded2caSGreg Roachuse Psr\Http\Message\ResponseInterface;
31e8ded2caSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32e8ded2caSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33e8ded2caSGreg Roach
34e8ded2caSGreg Roachuse function assert;
35e8ded2caSGreg Roachuse function is_string;
36e8ded2caSGreg Roachuse function redirect;
37e8ded2caSGreg Roach
38e8ded2caSGreg Roach/**
39e8ded2caSGreg Roach * Show a location's page.
40e8ded2caSGreg Roach */
41e8ded2caSGreg Roachclass LocationPage implements RequestHandlerInterface
42e8ded2caSGreg Roach{
43e8ded2caSGreg Roach    use ViewResponseTrait;
44e8ded2caSGreg Roach
45*4991f205SGreg Roach    private ClipboardService $clipboard_service;
46*4991f205SGreg Roach
47*4991f205SGreg Roach    private LinkedRecordService $linked_record_service;
48*4991f205SGreg Roach
49*4991f205SGreg Roach    /**
50*4991f205SGreg Roach     * @param ClipboardService $clipboard_service
51*4991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
52*4991f205SGreg Roach     */
53*4991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
54*4991f205SGreg Roach    {
55*4991f205SGreg Roach        $this->clipboard_service     = $clipboard_service;
56*4991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
57*4991f205SGreg Roach    }
58*4991f205SGreg Roach
59e8ded2caSGreg Roach    /**
60e8ded2caSGreg Roach     * @param ServerRequestInterface $request
61e8ded2caSGreg Roach     *
62e8ded2caSGreg Roach     * @return ResponseInterface
63e8ded2caSGreg Roach     */
64e8ded2caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
65e8ded2caSGreg Roach    {
66b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
67b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
68b55cbc6bSGreg Roach        $slug   = Validator::attributes($request)->string('slug', '');
690f5fd22fSGreg Roach        $record = Registry::locationFactory()->make($xref, $tree);
700f5fd22fSGreg Roach        $record = Auth::checkLocationAccess($record, false);
71e8ded2caSGreg Roach
72e8ded2caSGreg Roach        // Redirect to correct xref/slug
73b55cbc6bSGreg Roach        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
740f5fd22fSGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
75e8ded2caSGreg Roach        }
76e8ded2caSGreg Roach
770f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
78*4991f205SGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
79*4991f205SGreg Roach            'linked_families'      => $this->linked_record_service->linkedFamilies($record),
80*4991f205SGreg Roach            'linked_individuals'   => $this->linked_record_service->linkedIndividuals($record),
81*4991f205SGreg Roach            'linked_locations'     => $this->linked_record_service->linkedLocations($record),
820f5fd22fSGreg Roach            'linked_media_objects' => null,
830f5fd22fSGreg Roach            'linked_notes'         => null,
84*4991f205SGreg Roach            'linked_repositories'  => null,
85*4991f205SGreg Roach            'linked_sources'       => $this->linked_record_service->linkedSources($record),
860f5fd22fSGreg Roach            'record'               => $record,
870f5fd22fSGreg Roach            'title'                => $record->fullName(),
88e8ded2caSGreg Roach            'tree'                 => $tree,
89e8ded2caSGreg Roach        ]);
90e8ded2caSGreg Roach    }
91e8ded2caSGreg Roach}
92