xref: /webtrees/app/Http/RequestHandlers/FixLevel0MediaAction.php (revision 4b3ef6caf72914d34581bb4a8d677e615e877a9b)
1*4b3ef6caSGreg Roach<?php
2*4b3ef6caSGreg Roach
3*4b3ef6caSGreg Roach/**
4*4b3ef6caSGreg Roach * webtrees: online genealogy
5*4b3ef6caSGreg Roach * Copyright (C) 2021 webtrees development team
6*4b3ef6caSGreg Roach * This program is free software: you can redistribute it and/or modify
7*4b3ef6caSGreg Roach * it under the terms of the GNU General Public License as published by
8*4b3ef6caSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*4b3ef6caSGreg Roach * (at your option) any later version.
10*4b3ef6caSGreg Roach * This program is distributed in the hope that it will be useful,
11*4b3ef6caSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*4b3ef6caSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*4b3ef6caSGreg Roach * GNU General Public License for more details.
14*4b3ef6caSGreg Roach * You should have received a copy of the GNU General Public License
15*4b3ef6caSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*4b3ef6caSGreg Roach */
17*4b3ef6caSGreg Roach
18*4b3ef6caSGreg Roachdeclare(strict_types=1);
19*4b3ef6caSGreg Roach
20*4b3ef6caSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*4b3ef6caSGreg Roach
22*4b3ef6caSGreg Roachuse Fisharebest\Webtrees\Registry;
23*4b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
24*4b3ef6caSGreg Roachuse Psr\Http\Message\ResponseInterface;
25*4b3ef6caSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26*4b3ef6caSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
27*4b3ef6caSGreg Roach
28*4b3ef6caSGreg Roachuse function response;
29*4b3ef6caSGreg Roach
30*4b3ef6caSGreg Roach/**
31*4b3ef6caSGreg Roach * Move media links from records to facts.
32*4b3ef6caSGreg Roach */
33*4b3ef6caSGreg Roachclass FixLevel0MediaAction implements RequestHandlerInterface
34*4b3ef6caSGreg Roach{
35*4b3ef6caSGreg Roach    /** @var TreeService */
36*4b3ef6caSGreg Roach    private $tree_service;
37*4b3ef6caSGreg Roach
38*4b3ef6caSGreg Roach    /**
39*4b3ef6caSGreg Roach     * FixLevel0MediaController constructor.
40*4b3ef6caSGreg Roach     *
41*4b3ef6caSGreg Roach     * @param TreeService $tree_service
42*4b3ef6caSGreg Roach     */
43*4b3ef6caSGreg Roach    public function __construct(TreeService $tree_service)
44*4b3ef6caSGreg Roach    {
45*4b3ef6caSGreg Roach        $this->tree_service = $tree_service;
46*4b3ef6caSGreg Roach    }
47*4b3ef6caSGreg Roach
48*4b3ef6caSGreg Roach    /**
49*4b3ef6caSGreg Roach     * Move a link to a media object from a level 0 record to a level 1 record.
50*4b3ef6caSGreg Roach     *
51*4b3ef6caSGreg Roach     * @param ServerRequestInterface $request
52*4b3ef6caSGreg Roach     *
53*4b3ef6caSGreg Roach     * @return ResponseInterface
54*4b3ef6caSGreg Roach     */
55*4b3ef6caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
56*4b3ef6caSGreg Roach    {
57*4b3ef6caSGreg Roach        $params = (array) $request->getParsedBody();
58*4b3ef6caSGreg Roach
59*4b3ef6caSGreg Roach        $fact_id   = $params['fact_id'];
60*4b3ef6caSGreg Roach        $indi_xref = $params['indi_xref'];
61*4b3ef6caSGreg Roach        $obje_xref = $params['obje_xref'];
62*4b3ef6caSGreg Roach        $tree_id   = (int) $params['tree_id'];
63*4b3ef6caSGreg Roach
64*4b3ef6caSGreg Roach        $tree       = $this->tree_service->find($tree_id);
65*4b3ef6caSGreg Roach        $individual = Registry::individualFactory()->make($indi_xref, $tree);
66*4b3ef6caSGreg Roach        $media      = Registry::mediaFactory()->make($obje_xref, $tree);
67*4b3ef6caSGreg Roach
68*4b3ef6caSGreg Roach        if ($individual !== null && $media !== null) {
69*4b3ef6caSGreg Roach            foreach ($individual->facts() as $fact1) {
70*4b3ef6caSGreg Roach                if ($fact1->id() === $fact_id) {
71*4b3ef6caSGreg Roach                    $individual->updateFact($fact_id, $fact1->gedcom() . "\n2 OBJE @" . $obje_xref . '@', false);
72*4b3ef6caSGreg Roach                    foreach ($individual->facts(['OBJE']) as $fact2) {
73*4b3ef6caSGreg Roach                        if ($fact2->target() === $media) {
74*4b3ef6caSGreg Roach                            $individual->deleteFact($fact2->id(), false);
75*4b3ef6caSGreg Roach                        }
76*4b3ef6caSGreg Roach                    }
77*4b3ef6caSGreg Roach                    break;
78*4b3ef6caSGreg Roach                }
79*4b3ef6caSGreg Roach            }
80*4b3ef6caSGreg Roach        }
81*4b3ef6caSGreg Roach
82*4b3ef6caSGreg Roach        return response();
83*4b3ef6caSGreg Roach    }
84*4b3ef6caSGreg Roach}
85