xref: /webtrees/app/Http/RequestHandlers/SharedNotePage.php (revision d11be7027e34e3121be11cc025421873364403f9)
1701f5d18SGreg Roach<?php
2701f5d18SGreg Roach
3701f5d18SGreg Roach/**
4701f5d18SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6701f5d18SGreg Roach * This program is free software: you can redistribute it and/or modify
7701f5d18SGreg Roach * it under the terms of the GNU General Public License as published by
8701f5d18SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9701f5d18SGreg Roach * (at your option) any later version.
10701f5d18SGreg Roach * This program is distributed in the hope that it will be useful,
11701f5d18SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12701f5d18SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13701f5d18SGreg Roach * GNU General Public License for more details.
14701f5d18SGreg Roach * You should have received a copy of the GNU General Public License
15701f5d18SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16701f5d18SGreg Roach */
17701f5d18SGreg Roach
18701f5d18SGreg Roachdeclare(strict_types=1);
19701f5d18SGreg Roach
20701f5d18SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21701f5d18SGreg Roach
22701f5d18SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23701f5d18SGreg Roachuse Fisharebest\Webtrees\Auth;
24701f5d18SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
25701f5d18SGreg Roachuse Fisharebest\Webtrees\Registry;
26701f5d18SGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
27701f5d18SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
28701f5d18SGreg Roachuse Fisharebest\Webtrees\Validator;
29701f5d18SGreg Roachuse Psr\Http\Message\ResponseInterface;
30701f5d18SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31701f5d18SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32701f5d18SGreg Roach
33701f5d18SGreg Roachuse function redirect;
34701f5d18SGreg Roach
35701f5d18SGreg Roach/**
36701f5d18SGreg Roach * Show a shared-note's page.
37701f5d18SGreg Roach */
38701f5d18SGreg Roachclass SharedNotePage implements RequestHandlerInterface
39701f5d18SGreg Roach{
40701f5d18SGreg Roach    use ViewResponseTrait;
41701f5d18SGreg Roach
42701f5d18SGreg Roach    private ClipboardService $clipboard_service;
43701f5d18SGreg Roach
44701f5d18SGreg Roach    private LinkedRecordService $linked_record_service;
45701f5d18SGreg Roach
46701f5d18SGreg Roach    /**
47701f5d18SGreg Roach     * @param ClipboardService $clipboard_service
48701f5d18SGreg Roach     * @param LinkedRecordService $linked_record_service
49701f5d18SGreg Roach     */
50701f5d18SGreg Roach    public function __construct(ClipboardService $clipboard_service, LinkedRecordService $linked_record_service)
51701f5d18SGreg Roach    {
52701f5d18SGreg Roach        $this->clipboard_service     = $clipboard_service;
53701f5d18SGreg Roach        $this->linked_record_service = $linked_record_service;
54701f5d18SGreg Roach    }
55701f5d18SGreg Roach
56701f5d18SGreg Roach    /**
57701f5d18SGreg Roach     * @param ServerRequestInterface $request
58701f5d18SGreg Roach     *
59701f5d18SGreg Roach     * @return ResponseInterface
60701f5d18SGreg Roach     */
61701f5d18SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
62701f5d18SGreg Roach    {
63701f5d18SGreg Roach        $tree   = Validator::attributes($request)->tree();
64701f5d18SGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
65701f5d18SGreg Roach        $slug   = Validator::attributes($request)->string('slug', '');
66701f5d18SGreg Roach        $record = Registry::sharedNoteFactory()->make($xref, $tree);
67701f5d18SGreg Roach        $record = Auth::checkSharedNoteAccess($record, false);
68701f5d18SGreg Roach
69701f5d18SGreg Roach        // Redirect to correct xref/slug
70701f5d18SGreg Roach        if ($record->xref() !== $xref || Registry::slugFactory()->make($record) !== $slug) {
71701f5d18SGreg Roach            return redirect($record->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
72701f5d18SGreg Roach        }
73701f5d18SGreg Roach
74701f5d18SGreg Roach        $linked_families     = $this->linked_record_service->linkedFamilies($record);
75701f5d18SGreg Roach        $linked_individuals  = $this->linked_record_service->linkedIndividuals($record);
76701f5d18SGreg Roach        $linked_locations    = $this->linked_record_service->linkedLocations($record);
77701f5d18SGreg Roach        $linked_media        = $this->linked_record_service->linkedMedia($record);
78701f5d18SGreg Roach        $linked_repositories = $this->linked_record_service->linkedRepositories($record);
79701f5d18SGreg Roach        $linked_sources      = $this->linked_record_service->linkedSources($record);
80701f5d18SGreg Roach        $linked_submitters   = $this->linked_record_service->linkedSubmitters($record);
81701f5d18SGreg Roach
82701f5d18SGreg Roach        return $this->viewResponse('shared-note-page', [
83701f5d18SGreg Roach            'clipboard_facts'      => $this->clipboard_service->pastableFacts($record),
84701f5d18SGreg Roach            'linked_families'      => $linked_families,
85701f5d18SGreg Roach            'linked_individuals'   => $linked_individuals,
86701f5d18SGreg Roach            'linked_locations'     => $linked_locations->isEmpty() ? null : $linked_locations,
87701f5d18SGreg Roach            'linked_media_objects' => $linked_media,
88701f5d18SGreg Roach            'linked_repositories'  => $linked_repositories,
89701f5d18SGreg Roach            'linked_sources'       => $linked_sources,
90701f5d18SGreg Roach            'linked_submitters'    => $linked_submitters,
91701f5d18SGreg Roach            'meta_description'     => '',
92701f5d18SGreg Roach            'meta_robots'          => 'index,follow',
93701f5d18SGreg Roach            'record'               => $record,
94701f5d18SGreg Roach            'title'                => $record->fullName(),
95701f5d18SGreg Roach            'tree'                 => $tree,
96701f5d18SGreg Roach        ]);
97701f5d18SGreg Roach    }
98701f5d18SGreg Roach}
99