xref: /webtrees/app/Http/RequestHandlers/LinkMediaToRecordAction.php (revision ddb44b4cf356ab8fd0c5d21becf3fce4c4e46244)
1*ddb44b4cSGreg Roach<?php
2*ddb44b4cSGreg Roach
3*ddb44b4cSGreg Roach/**
4*ddb44b4cSGreg Roach * webtrees: online genealogy
5*ddb44b4cSGreg Roach * Copyright (C) 2021 webtrees development team
6*ddb44b4cSGreg Roach * This program is free software: you can redistribute it and/or modify
7*ddb44b4cSGreg Roach * it under the terms of the GNU General Public License as published by
8*ddb44b4cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*ddb44b4cSGreg Roach * (at your option) any later version.
10*ddb44b4cSGreg Roach * This program is distributed in the hope that it will be useful,
11*ddb44b4cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*ddb44b4cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*ddb44b4cSGreg Roach * GNU General Public License for more details.
14*ddb44b4cSGreg Roach * You should have received a copy of the GNU General Public License
15*ddb44b4cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*ddb44b4cSGreg Roach */
17*ddb44b4cSGreg Roach
18*ddb44b4cSGreg Roachdeclare(strict_types=1);
19*ddb44b4cSGreg Roach
20*ddb44b4cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*ddb44b4cSGreg Roach
22*ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Auth;
23*ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Registry;
24*ddb44b4cSGreg Roachuse Fisharebest\Webtrees\Tree;
25*ddb44b4cSGreg Roachuse Psr\Http\Message\ResponseInterface;
26*ddb44b4cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27*ddb44b4cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28*ddb44b4cSGreg Roach
29*ddb44b4cSGreg Roachuse function assert;
30*ddb44b4cSGreg Roachuse function is_string;
31*ddb44b4cSGreg Roach
32*ddb44b4cSGreg Roach/**
33*ddb44b4cSGreg Roach * Link a media object to a record.
34*ddb44b4cSGreg Roach */
35*ddb44b4cSGreg Roachclass LinkMediaToRecordAction implements RequestHandlerInterface
36*ddb44b4cSGreg Roach{
37*ddb44b4cSGreg Roach    /**
38*ddb44b4cSGreg Roach     * @param ServerRequestInterface $request
39*ddb44b4cSGreg Roach     *
40*ddb44b4cSGreg Roach     * @return ResponseInterface
41*ddb44b4cSGreg Roach     */
42*ddb44b4cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
43*ddb44b4cSGreg Roach    {
44*ddb44b4cSGreg Roach        $tree = $request->getAttribute('tree');
45*ddb44b4cSGreg Roach        assert($tree instanceof Tree);
46*ddb44b4cSGreg Roach
47*ddb44b4cSGreg Roach        $xref = $request->getAttribute('xref');
48*ddb44b4cSGreg Roach        assert(is_string($xref));
49*ddb44b4cSGreg Roach
50*ddb44b4cSGreg Roach        $params = (array) $request->getParsedBody();
51*ddb44b4cSGreg Roach        $link   = $params['link'] ?? '';
52*ddb44b4cSGreg Roach
53*ddb44b4cSGreg Roach        $media = Registry::mediaFactory()->make($xref, $tree);
54*ddb44b4cSGreg Roach        $media = Auth::checkMediaAccess($media);
55*ddb44b4cSGreg Roach
56*ddb44b4cSGreg Roach        $record = Registry::gedcomRecordFactory()->make($link, $tree);
57*ddb44b4cSGreg Roach        $record = Auth::checkRecordAccess($record, true);
58*ddb44b4cSGreg Roach
59*ddb44b4cSGreg Roach        $record->createFact('1 OBJE @' . $xref . '@', true);
60*ddb44b4cSGreg Roach
61*ddb44b4cSGreg Roach        return redirect($media->url());
62*ddb44b4cSGreg Roach    }
63*ddb44b4cSGreg Roach}
64