xref: /webtrees/app/Http/RequestHandlers/MergeRecordsAction.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
15bbfbb82SGreg Roach<?php
25bbfbb82SGreg Roach
35bbfbb82SGreg Roach/**
45bbfbb82SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
65bbfbb82SGreg Roach * This program is free software: you can redistribute it and/or modify
75bbfbb82SGreg Roach * it under the terms of the GNU General Public License as published by
85bbfbb82SGreg Roach * the Free Software Foundation, either version 3 of the License, or
95bbfbb82SGreg Roach * (at your option) any later version.
105bbfbb82SGreg Roach * This program is distributed in the hope that it will be useful,
115bbfbb82SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
125bbfbb82SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135bbfbb82SGreg Roach * GNU General Public License for more details.
145bbfbb82SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
165bbfbb82SGreg Roach */
175bbfbb82SGreg Roach
185bbfbb82SGreg Roachdeclare(strict_types=1);
195bbfbb82SGreg Roach
205bbfbb82SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
215bbfbb82SGreg Roach
226b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
23*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
245bbfbb82SGreg Roachuse Psr\Http\Message\ResponseInterface;
255bbfbb82SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
265bbfbb82SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
275bbfbb82SGreg Roach
285bbfbb82SGreg Roachuse function redirect;
295bbfbb82SGreg Roachuse function route;
305bbfbb82SGreg Roach
315bbfbb82SGreg Roach/**
325bbfbb82SGreg Roach * Merge records
335bbfbb82SGreg Roach */
345bbfbb82SGreg Roachclass MergeRecordsAction implements RequestHandlerInterface
355bbfbb82SGreg Roach{
365bbfbb82SGreg Roach    /**
375bbfbb82SGreg Roach     * @param ServerRequestInterface $request
385bbfbb82SGreg Roach     *
395bbfbb82SGreg Roach     * @return ResponseInterface
405bbfbb82SGreg Roach     */
415bbfbb82SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
425bbfbb82SGreg Roach    {
43*b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
445bbfbb82SGreg Roach
45b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
46b46c87bdSGreg Roach
47b46c87bdSGreg Roach        $xref1 = $params['xref1'] ?? '';
48b46c87bdSGreg Roach        $xref2 = $params['xref2'] ?? '';
495bbfbb82SGreg Roach
505bbfbb82SGreg Roach        // Merge record2 into record1
516b9cb339SGreg Roach        $record1 = Registry::gedcomRecordFactory()->make($xref1, $tree);
526b9cb339SGreg Roach        $record2 = Registry::gedcomRecordFactory()->make($xref2, $tree);
535bbfbb82SGreg Roach
545bbfbb82SGreg Roach        if (
555bbfbb82SGreg Roach            $record1 === null ||
565bbfbb82SGreg Roach            $record2 === null ||
575bbfbb82SGreg Roach            $record1 === $record2 ||
5802467d32SGreg Roach            $record1->tag() !== $record2->tag() ||
595bbfbb82SGreg Roach            $record1->isPendingDeletion() ||
605bbfbb82SGreg Roach            $record2->isPendingDeletion()
615bbfbb82SGreg Roach        ) {
625bbfbb82SGreg Roach            return redirect(route(MergeRecordsPage::class, [
635bbfbb82SGreg Roach                'tree'  => $tree->name(),
645bbfbb82SGreg Roach                'xref1' => $xref1,
655bbfbb82SGreg Roach                'xref2' => $xref2,
665bbfbb82SGreg Roach            ]));
675bbfbb82SGreg Roach        }
685bbfbb82SGreg Roach
695bbfbb82SGreg Roach        return redirect(route(MergeFactsPage::class, [
705bbfbb82SGreg Roach            'tree'  => $tree->name(),
715bbfbb82SGreg Roach            'xref1' => $xref1,
725bbfbb82SGreg Roach            'xref2' => $xref2,
735bbfbb82SGreg Roach        ]));
745bbfbb82SGreg Roach    }
755bbfbb82SGreg Roach}
76