xref: /webtrees/app/Http/RequestHandlers/FixLevel0MediaAction.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
14b3ef6caSGreg Roach<?php
24b3ef6caSGreg Roach
34b3ef6caSGreg Roach/**
44b3ef6caSGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
64b3ef6caSGreg Roach * This program is free software: you can redistribute it and/or modify
74b3ef6caSGreg Roach * it under the terms of the GNU General Public License as published by
84b3ef6caSGreg Roach * the Free Software Foundation, either version 3 of the License, or
94b3ef6caSGreg Roach * (at your option) any later version.
104b3ef6caSGreg Roach * This program is distributed in the hope that it will be useful,
114b3ef6caSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124b3ef6caSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134b3ef6caSGreg Roach * GNU General Public License for more details.
144b3ef6caSGreg Roach * You should have received a copy of the GNU General Public License
154b3ef6caSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164b3ef6caSGreg Roach */
174b3ef6caSGreg Roach
184b3ef6caSGreg Roachdeclare(strict_types=1);
194b3ef6caSGreg Roach
204b3ef6caSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
214b3ef6caSGreg Roach
224b3ef6caSGreg Roachuse Fisharebest\Webtrees\Registry;
234b3ef6caSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
244b3ef6caSGreg Roachuse Psr\Http\Message\ResponseInterface;
254b3ef6caSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
264b3ef6caSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
274b3ef6caSGreg Roach
284b3ef6caSGreg Roachuse function response;
294b3ef6caSGreg Roach
304b3ef6caSGreg Roach/**
314b3ef6caSGreg Roach * Move media links from records to facts.
324b3ef6caSGreg Roach */
334b3ef6caSGreg Roachclass FixLevel0MediaAction implements RequestHandlerInterface
344b3ef6caSGreg Roach{
35c4943cffSGreg Roach    private TreeService $tree_service;
364b3ef6caSGreg Roach
374b3ef6caSGreg Roach    /**
384b3ef6caSGreg Roach     * FixLevel0MediaController constructor.
394b3ef6caSGreg Roach     *
404b3ef6caSGreg Roach     * @param TreeService $tree_service
414b3ef6caSGreg Roach     */
424b3ef6caSGreg Roach    public function __construct(TreeService $tree_service)
434b3ef6caSGreg Roach    {
444b3ef6caSGreg Roach        $this->tree_service = $tree_service;
454b3ef6caSGreg Roach    }
464b3ef6caSGreg Roach
474b3ef6caSGreg Roach    /**
484b3ef6caSGreg Roach     * Move a link to a media object from a level 0 record to a level 1 record.
494b3ef6caSGreg Roach     *
504b3ef6caSGreg Roach     * @param ServerRequestInterface $request
514b3ef6caSGreg Roach     *
524b3ef6caSGreg Roach     * @return ResponseInterface
534b3ef6caSGreg Roach     */
544b3ef6caSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
554b3ef6caSGreg Roach    {
564b3ef6caSGreg Roach        $params = (array) $request->getParsedBody();
574b3ef6caSGreg Roach
584b3ef6caSGreg Roach        $fact_id   = $params['fact_id'];
594b3ef6caSGreg Roach        $indi_xref = $params['indi_xref'];
604b3ef6caSGreg Roach        $obje_xref = $params['obje_xref'];
614b3ef6caSGreg Roach        $tree_id   = (int) $params['tree_id'];
624b3ef6caSGreg Roach
634b3ef6caSGreg Roach        $tree       = $this->tree_service->find($tree_id);
644b3ef6caSGreg Roach        $individual = Registry::individualFactory()->make($indi_xref, $tree);
654b3ef6caSGreg Roach        $media      = Registry::mediaFactory()->make($obje_xref, $tree);
664b3ef6caSGreg Roach
674b3ef6caSGreg Roach        if ($individual !== null && $media !== null) {
684b3ef6caSGreg Roach            foreach ($individual->facts() as $fact1) {
694b3ef6caSGreg Roach                if ($fact1->id() === $fact_id) {
704b3ef6caSGreg Roach                    $individual->updateFact($fact_id, $fact1->gedcom() . "\n2 OBJE @" . $obje_xref . '@', false);
714b3ef6caSGreg Roach                    foreach ($individual->facts(['OBJE']) as $fact2) {
724b3ef6caSGreg Roach                        if ($fact2->target() === $media) {
734b3ef6caSGreg Roach                            $individual->deleteFact($fact2->id(), false);
744b3ef6caSGreg Roach                        }
754b3ef6caSGreg Roach                    }
764b3ef6caSGreg Roach                    break;
774b3ef6caSGreg Roach                }
784b3ef6caSGreg Roach            }
794b3ef6caSGreg Roach        }
804b3ef6caSGreg Roach
814b3ef6caSGreg Roach        return response();
824b3ef6caSGreg Roach    }
834b3ef6caSGreg Roach}
84