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