xref: /webtrees/app/Http/RequestHandlers/MergeFactsPage.php (revision 541cf44d763a8aa5906f3fce58ca6b0ac3a25986)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Validator;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function redirect;
31
32/**
33 * Merge records
34 */
35class MergeFactsPage implements RequestHandlerInterface
36{
37    use ViewResponseTrait;
38
39    /**
40     * Merge two genealogy records.
41     *
42     * @param ServerRequestInterface $request
43     *
44     * @return ResponseInterface
45     */
46    public function handle(ServerRequestInterface $request): ResponseInterface
47    {
48        $this->layout = 'layouts/administration';
49
50        $tree = Validator::attributes($request)->tree();
51
52        $xref1 = $request->getQueryParams()['xref1'] ?? '';
53        $xref2 = $request->getQueryParams()['xref2'] ?? '';
54
55        $title = I18N::translate('Merge records') . ' — ' . e($tree->title());
56
57        $record1 = Registry::gedcomRecordFactory()->make($xref1, $tree);
58        $record2 = Registry::gedcomRecordFactory()->make($xref2, $tree);
59
60        if (
61            $record1 === null ||
62            $record2 === null ||
63            $record1 === $record2 ||
64            $record1->tag() !== $record2->tag() ||
65            $record1->isPendingDeletion() ||
66            $record2->isPendingDeletion()
67        ) {
68            return redirect(route(MergeRecordsPage::class, [
69                'tree'  => $tree->name(),
70                'xref1' => $xref1,
71                'xref2' => $xref2,
72            ]));
73        }
74
75        // Facts found both records
76        $facts = [];
77
78        // Facts found in only one record
79        $facts1 = [];
80        $facts2 = [];
81
82        foreach ($record1->facts() as $fact) {
83            if (!$fact->isPendingDeletion() && !str_ends_with($fact->tag(), ':CHAN')) {
84                $facts1[$fact->id()] = $fact;
85            }
86        }
87
88        foreach ($record2->facts() as $fact) {
89            if (!$fact->isPendingDeletion() && !str_ends_with($fact->tag(), ':CHAN')) {
90                $facts2[$fact->id()] = $fact;
91            }
92        }
93
94        foreach ($facts1 as $id1 => $fact1) {
95            foreach ($facts2 as $id2 => $fact2) {
96                if ($fact1->id() === $fact2->id()) {
97                    $facts[] = $fact1;
98                    unset($facts1[$id1], $facts2[$id2]);
99                }
100            }
101        }
102
103        return $this->viewResponse('admin/merge-records-step-2', [
104            'facts'   => $facts,
105            'facts1'  => $facts1,
106            'facts2'  => $facts2,
107            'record1' => $record1,
108            'record2' => $record2,
109            'title'   => $title,
110            'tree'    => $tree,
111        ]);
112    }
113}
114