xref: /webtrees/app/Http/RequestHandlers/MergeFactsPage.php (revision d11be7027e34e3121be11cc025421873364403f9)
15bbfbb82SGreg Roach<?php
25bbfbb82SGreg Roach
35bbfbb82SGreg Roach/**
45bbfbb82SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
225bbfbb82SGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
235bbfbb82SGreg Roachuse Fisharebest\Webtrees\I18N;
246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
265bbfbb82SGreg Roachuse Psr\Http\Message\ResponseInterface;
275bbfbb82SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
285bbfbb82SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
295bbfbb82SGreg Roach
305bbfbb82SGreg Roachuse function redirect;
315bbfbb82SGreg Roach
325bbfbb82SGreg Roach/**
335bbfbb82SGreg Roach * Merge records
345bbfbb82SGreg Roach */
355bbfbb82SGreg Roachclass MergeFactsPage implements RequestHandlerInterface
365bbfbb82SGreg Roach{
375bbfbb82SGreg Roach    use ViewResponseTrait;
385bbfbb82SGreg Roach
395bbfbb82SGreg Roach    /**
405bbfbb82SGreg Roach     * Merge two genealogy records.
415bbfbb82SGreg Roach     *
425bbfbb82SGreg Roach     * @param ServerRequestInterface $request
435bbfbb82SGreg Roach     *
445bbfbb82SGreg Roach     * @return ResponseInterface
455bbfbb82SGreg Roach     */
465bbfbb82SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
475bbfbb82SGreg Roach    {
485bbfbb82SGreg Roach        $this->layout = 'layouts/administration';
495bbfbb82SGreg Roach
50b55cbc6bSGreg Roach        $tree  = Validator::attributes($request)->tree();
51748dbe15SGreg Roach        $xref1 = Validator::queryParams($request)->isXref()->string('xref1');
52748dbe15SGreg Roach        $xref2 = Validator::queryParams($request)->isXref()->string('xref2');
535bbfbb82SGreg Roach        $title = I18N::translate('Merge records') . ' — ' . e($tree->title());
545bbfbb82SGreg Roach
556b9cb339SGreg Roach        $record1 = Registry::gedcomRecordFactory()->make($xref1, $tree);
566b9cb339SGreg Roach        $record2 = Registry::gedcomRecordFactory()->make($xref2, $tree);
575bbfbb82SGreg Roach
585bbfbb82SGreg Roach        if (
595bbfbb82SGreg Roach            $record1 === null ||
605bbfbb82SGreg Roach            $record2 === null ||
615bbfbb82SGreg Roach            $record1 === $record2 ||
6202467d32SGreg Roach            $record1->tag() !== $record2->tag() ||
635bbfbb82SGreg Roach            $record1->isPendingDeletion() ||
645bbfbb82SGreg Roach            $record2->isPendingDeletion()
655bbfbb82SGreg Roach        ) {
665bbfbb82SGreg Roach            return redirect(route(MergeRecordsPage::class, [
675bbfbb82SGreg Roach                'tree'  => $tree->name(),
685bbfbb82SGreg Roach                'xref1' => $xref1,
695bbfbb82SGreg Roach                'xref2' => $xref2,
705bbfbb82SGreg Roach            ]));
715bbfbb82SGreg Roach        }
725bbfbb82SGreg Roach
735bbfbb82SGreg Roach        // Facts found both records
745bbfbb82SGreg Roach        $facts = [];
755bbfbb82SGreg Roach
765bbfbb82SGreg Roach        // Facts found in only one record
775bbfbb82SGreg Roach        $facts1 = [];
785bbfbb82SGreg Roach        $facts2 = [];
795bbfbb82SGreg Roach
805bbfbb82SGreg Roach        foreach ($record1->facts() as $fact) {
81d0889c63SGreg Roach            if (!$fact->isPendingDeletion() && !str_ends_with($fact->tag(), ':CHAN')) {
825bbfbb82SGreg Roach                $facts1[$fact->id()] = $fact;
835bbfbb82SGreg Roach            }
845bbfbb82SGreg Roach        }
855bbfbb82SGreg Roach
865bbfbb82SGreg Roach        foreach ($record2->facts() as $fact) {
87d0889c63SGreg Roach            if (!$fact->isPendingDeletion() && !str_ends_with($fact->tag(), ':CHAN')) {
885bbfbb82SGreg Roach                $facts2[$fact->id()] = $fact;
895bbfbb82SGreg Roach            }
905bbfbb82SGreg Roach        }
915bbfbb82SGreg Roach
925bbfbb82SGreg Roach        foreach ($facts1 as $id1 => $fact1) {
935bbfbb82SGreg Roach            foreach ($facts2 as $id2 => $fact2) {
945bbfbb82SGreg Roach                if ($fact1->id() === $fact2->id()) {
955bbfbb82SGreg Roach                    $facts[] = $fact1;
965bbfbb82SGreg Roach                    unset($facts1[$id1], $facts2[$id2]);
975bbfbb82SGreg Roach                }
985bbfbb82SGreg Roach            }
995bbfbb82SGreg Roach        }
1005bbfbb82SGreg Roach
1015bbfbb82SGreg Roach        return $this->viewResponse('admin/merge-records-step-2', [
1025bbfbb82SGreg Roach            'facts'   => $facts,
1035bbfbb82SGreg Roach            'facts1'  => $facts1,
1045bbfbb82SGreg Roach            'facts2'  => $facts2,
1055bbfbb82SGreg Roach            'record1' => $record1,
1065bbfbb82SGreg Roach            'record2' => $record2,
1075bbfbb82SGreg Roach            'title'   => $title,
1085bbfbb82SGreg Roach            'tree'    => $tree,
1095bbfbb82SGreg Roach        ]);
1105bbfbb82SGreg Roach    }
1115bbfbb82SGreg Roach}
112