xref: /webtrees/app/Http/RequestHandlers/RepositoryPage.php (revision 4991f2057a6647447a648c5d6743dab00378e98e)
154186344SGreg Roach<?php
254186344SGreg Roach
354186344SGreg Roach/**
454186344SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
654186344SGreg Roach * This program is free software: you can redistribute it and/or modify
754186344SGreg Roach * it under the terms of the GNU General Public License as published by
854186344SGreg Roach * the Free Software Foundation, either version 3 of the License, or
954186344SGreg Roach * (at your option) any later version.
1054186344SGreg Roach * This program is distributed in the hope that it will be useful,
1154186344SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1254186344SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1354186344SGreg Roach * GNU General Public License for more details.
1454186344SGreg Roach * 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/>.
1654186344SGreg Roach */
1754186344SGreg Roach
1854186344SGreg Roachdeclare(strict_types=1);
1954186344SGreg Roach
2054186344SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2154186344SGreg Roach
22e5d858f5SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2354186344SGreg Roachuse Fisharebest\Webtrees\Auth;
2454186344SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
2654186344SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
27*4991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2954186344SGreg Roachuse Psr\Http\Message\ResponseInterface;
3054186344SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3154186344SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3254186344SGreg Roach
3354186344SGreg Roachuse function redirect;
3454186344SGreg Roach
3554186344SGreg Roach/**
3654186344SGreg Roach * Show a repository's page.
3754186344SGreg Roach */
3854186344SGreg Roachclass RepositoryPage implements RequestHandlerInterface
3954186344SGreg Roach{
4054186344SGreg Roach    use ViewResponseTrait;
4154186344SGreg Roach
42c4943cffSGreg Roach    private ClipboardService $clipboard_service;
4354186344SGreg Roach
44*4991f205SGreg Roach    private LinkedRecordService $linked_record_service;
45*4991f205SGreg Roach
4654186344SGreg Roach    /**
4754186344SGreg Roach     * @param ClipboardService $clipboard_service
48*4991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
4954186344SGreg Roach     */
50*4991f205SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
5154186344SGreg Roach    {
5254186344SGreg Roach        $this->clipboard_service     = $clipboard_service;
53*4991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
5454186344SGreg Roach    }
5554186344SGreg Roach
5654186344SGreg Roach    /**
5754186344SGreg Roach     * @param ServerRequestInterface $request
5854186344SGreg Roach     *
5954186344SGreg Roach     * @return ResponseInterface
6054186344SGreg Roach     */
6154186344SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
6254186344SGreg 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::repositoryFactory()->make($xref, $tree);
670f5fd22fSGreg Roach        $record = Auth::checkRepositoryAccess($record, false);
6854186344SGreg Roach
6954186344SGreg 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);
7254186344SGreg Roach        }
7354186344SGreg Roach
74*4991f205SGreg Roach        $linked_sources = $this->linked_record_service->linkedSources($record);
75*4991f205SGreg Roach
760f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
770f5fd22fSGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
780f5fd22fSGreg Roach            'linked_families'      => null,
790f5fd22fSGreg Roach            'linked_individuals'   => null,
80*4991f205SGreg Roach            'linked_locations'     => null,
810f5fd22fSGreg Roach            'linked_media_objects' => null,
820f5fd22fSGreg Roach            'linked_notes'         => null,
83*4991f205SGreg Roach            'linked_repositories'  => null,
84*4991f205SGreg Roach            'linked_sources'       => $linked_sources,
852406e0e0SGreg Roach            'meta_description'     => '',
8654186344SGreg Roach            'meta_robots'          => 'index,follow',
870f5fd22fSGreg Roach            'record'               => $record,
880f5fd22fSGreg Roach            'title'                => $record->fullName(),
8954186344SGreg Roach            'tree'                 => $tree,
9054186344SGreg Roach        ]);
9154186344SGreg Roach    }
9254186344SGreg Roach}
93