xref: /webtrees/app/Http/RequestHandlers/MergeFactsPage.php (revision f4c767fd89cdb62ee54edec032285924cd767af7)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Factory;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Tree;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function assert;
31use function redirect;
32
33/**
34 * Merge records
35 */
36class MergeFactsPage implements RequestHandlerInterface
37{
38    use ViewResponseTrait;
39
40    /**
41     * Merge two genealogy records.
42     *
43     * @param ServerRequestInterface $request
44     *
45     * @return ResponseInterface
46     */
47    public function handle(ServerRequestInterface $request): ResponseInterface
48    {
49        $this->layout = 'layouts/administration';
50
51        $tree = $request->getAttribute('tree');
52        assert($tree instanceof Tree);
53
54        $xref1 = $request->getQueryParams()['xref1'] ?? '';
55        $xref2 = $request->getQueryParams()['xref2'] ?? '';
56
57        $title = I18N::translate('Merge records') . ' — ' . e($tree->title());
58
59        $record1 = Factory::gedcomRecord()->make($xref1, $tree);
60        $record2 = Factory::gedcomRecord()->make($xref2, $tree);
61
62        if (
63            $record1 === null ||
64            $record2 === null ||
65            $record1 === $record2 ||
66            $record1::RECORD_TYPE !== $record2::RECORD_TYPE ||
67            $record1->isPendingDeletion() ||
68            $record2->isPendingDeletion()
69        ) {
70            return redirect(route(MergeRecordsPage::class, [
71                'tree'  => $tree->name(),
72                'xref1' => $xref1,
73                'xref2' => $xref2,
74            ]));
75        }
76
77        // Facts found both records
78        $facts = [];
79
80        // Facts found in only one record
81        $facts1 = [];
82        $facts2 = [];
83
84        foreach ($record1->facts() as $fact) {
85            if (!$fact->isPendingDeletion() && $fact->tag() !== 'CHAN') {
86                $facts1[$fact->id()] = $fact;
87            }
88        }
89
90        foreach ($record2->facts() as $fact) {
91            if (!$fact->isPendingDeletion() && $fact->tag() !== 'CHAN') {
92                $facts2[$fact->id()] = $fact;
93            }
94        }
95
96        foreach ($facts1 as $id1 => $fact1) {
97            foreach ($facts2 as $id2 => $fact2) {
98                if ($fact1->id() === $fact2->id()) {
99                    $facts[] = $fact1;
100                    unset($facts1[$id1], $facts2[$id2]);
101                }
102            }
103        }
104
105        return $this->viewResponse('admin/merge-records-step-2', [
106            'facts'   => $facts,
107            'facts1'  => $facts1,
108            'facts2'  => $facts2,
109            'record1' => $record1,
110            'record2' => $record2,
111            'title'   => $title,
112            'tree'    => $tree,
113        ]);
114    }
115}
116