xref: /webtrees/app/Http/RequestHandlers/RepositoryPage.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
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*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2854186344SGreg Roachuse Psr\Http\Message\ResponseInterface;
2954186344SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3054186344SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3154186344SGreg Roach
3254186344SGreg Roachuse function redirect;
3354186344SGreg Roach
3454186344SGreg Roach/**
3554186344SGreg Roach * Show a repository's page.
3654186344SGreg Roach */
3754186344SGreg Roachclass RepositoryPage implements RequestHandlerInterface
3854186344SGreg Roach{
3954186344SGreg Roach    use ViewResponseTrait;
4054186344SGreg Roach
41c4943cffSGreg Roach    private ClipboardService $clipboard_service;
4254186344SGreg Roach
4354186344SGreg Roach    /**
4454186344SGreg Roach     * RepositoryPage constructor.
4554186344SGreg Roach     *
4654186344SGreg Roach     * @param ClipboardService $clipboard_service
4754186344SGreg Roach     */
4854186344SGreg Roach    public function __construct(ClipboardService $clipboard_service)
4954186344SGreg Roach    {
5054186344SGreg Roach        $this->clipboard_service = $clipboard_service;
5154186344SGreg Roach    }
5254186344SGreg Roach
5354186344SGreg Roach    /**
5454186344SGreg Roach     * @param ServerRequestInterface $request
5554186344SGreg Roach     *
5654186344SGreg Roach     * @return ResponseInterface
5754186344SGreg Roach     */
5854186344SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
5954186344SGreg Roach    {
60*b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
61*b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
62*b55cbc6bSGreg Roach        $slug   = Validator::attributes($request)->string('slug', '');
630f5fd22fSGreg Roach        $record = Registry::repositoryFactory()->make($xref, $tree);
640f5fd22fSGreg Roach        $record = Auth::checkRepositoryAccess($record, false);
6554186344SGreg Roach
6654186344SGreg Roach        // Redirect to correct xref/slug
67*b55cbc6bSGreg Roach        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
680f5fd22fSGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
6954186344SGreg Roach        }
7054186344SGreg Roach
710f5fd22fSGreg Roach        return $this->viewResponse('record-page', [
720f5fd22fSGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
730f5fd22fSGreg Roach            'linked_families'      => null,
740f5fd22fSGreg Roach            'linked_individuals'   => null,
750f5fd22fSGreg Roach            'linked_media_objects' => null,
760f5fd22fSGreg Roach            'linked_notes'         => null,
770f5fd22fSGreg Roach            'linked_sources'       => $record->linkedSources('REPO'),
782406e0e0SGreg Roach            'meta_description'     => '',
7954186344SGreg Roach            'meta_robots'          => 'index,follow',
800f5fd22fSGreg Roach            'record'               => $record,
810f5fd22fSGreg Roach            'title'                => $record->fullName(),
8254186344SGreg Roach            'tree'                 => $tree,
8354186344SGreg Roach        ]);
8454186344SGreg Roach    }
8554186344SGreg Roach}
86