xref: /webtrees/app/Http/RequestHandlers/LocationPage.php (revision d11be7027e34e3121be11cc025421873364403f9)
1e8ded2caSGreg Roach<?php
2e8ded2caSGreg Roach
3e8ded2caSGreg Roach/**
4e8ded2caSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
264991f205SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
274991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29e8ded2caSGreg Roachuse Psr\Http\Message\ResponseInterface;
30e8ded2caSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31e8ded2caSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32e8ded2caSGreg Roach
33e8ded2caSGreg Roachuse function redirect;
34e8ded2caSGreg Roach
35e8ded2caSGreg Roach/**
36e8ded2caSGreg Roach * Show a location's page.
37e8ded2caSGreg Roach */
38e8ded2caSGreg Roachclass LocationPage implements RequestHandlerInterface
39e8ded2caSGreg Roach{
40e8ded2caSGreg Roach    use ViewResponseTrait;
41e8ded2caSGreg Roach
424991f205SGreg Roach    private ClipboardService $clipboard_service;
434991f205SGreg Roach
444991f205SGreg Roach    private LinkedRecordService $linked_record_service;
454991f205SGreg Roach
464991f205SGreg Roach    /**
474991f205SGreg Roach     * @param ClipboardService $clipboard_service
484991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
494991f205SGreg Roach     */
504991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
514991f205SGreg Roach    {
524991f205SGreg Roach        $this->clipboard_service     = $clipboard_service;
534991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
544991f205SGreg Roach    }
554991f205SGreg Roach
56e8ded2caSGreg Roach    /**
57e8ded2caSGreg Roach     * @param ServerRequestInterface $request
58e8ded2caSGreg Roach     *
59e8ded2caSGreg Roach     * @return ResponseInterface
60e8ded2caSGreg Roach     */
61e8ded2caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
62e8ded2caSGreg Roach    {
63b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
64b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
65b55cbc6bSGreg Roach        $slug   = Validator::attributes($request)->string('slug', '');
660f5fd22fSGreg Roach        $record = Registry::locationFactory()->make($xref, $tree);
670f5fd22fSGreg Roach        $record = Auth::checkLocationAccess($record, false);
68e8ded2caSGreg Roach
69e8ded2caSGreg Roach        // Redirect to correct xref/slug
70b55cbc6bSGreg Roach        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
710f5fd22fSGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
72e8ded2caSGreg Roach        }
73e8ded2caSGreg Roach
740f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
754991f205SGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
764991f205SGreg Roach            'linked_families'      => $this->linked_record_service->linkedFamilies($record),
774991f205SGreg Roach            'linked_individuals'   => $this->linked_record_service->linkedIndividuals($record),
784991f205SGreg Roach            'linked_locations'     => $this->linked_record_service->linkedLocations($record),
790f5fd22fSGreg Roach            'linked_media_objects' => null,
800f5fd22fSGreg Roach            'linked_notes'         => null,
814991f205SGreg Roach            'linked_repositories'  => null,
824991f205SGreg Roach            'linked_sources'       => $this->linked_record_service->linkedSources($record),
835962a3c2SGreg Roach            'linked_submitters'    => null,
840f5fd22fSGreg Roach            'record'               => $record,
850f5fd22fSGreg Roach            'title'                => $record->fullName(),
86e8ded2caSGreg Roach            'tree'                 => $tree,
87e8ded2caSGreg Roach        ]);
88e8ded2caSGreg Roach    }
89e8ded2caSGreg Roach}
90