xref: /webtrees/app/Http/RequestHandlers/MergeFactsPage.php (revision fca408422158df832c5a18485ea01e348aebd21a)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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        $xref1 = Validator::queryParams($request)->isXref()->string('xref1');
52        $xref2 = Validator::queryParams($request)->isXref()->string('xref2');
53        $title = I18N::translate('Merge records') . ' — ' . e($tree->title());
54
55        $record1 = Registry::gedcomRecordFactory()->make($xref1, $tree);
56        $record2 = Registry::gedcomRecordFactory()->make($xref2, $tree);
57
58        if (
59            $record1 === null ||
60            $record2 === null ||
61            $record1 === $record2 ||
62            $record1->tag() !== $record2->tag() ||
63            $record1->isPendingDeletion() ||
64            $record2->isPendingDeletion()
65        ) {
66            return redirect(route(MergeRecordsPage::class, [
67                'tree'  => $tree->name(),
68                'xref1' => $xref1,
69                'xref2' => $xref2,
70            ]));
71        }
72
73        // Facts found both records
74        $facts = [];
75
76        // Facts found in only one record
77        $facts1 = [];
78        $facts2 = [];
79
80        foreach ($record1->facts() as $fact) {
81            if (!$fact->isPendingDeletion() && !str_ends_with($fact->tag(), ':CHAN')) {
82                $facts1[$fact->id()] = $fact;
83            }
84        }
85
86        foreach ($record2->facts() as $fact) {
87            if (!$fact->isPendingDeletion() && !str_ends_with($fact->tag(), ':CHAN')) {
88                $facts2[$fact->id()] = $fact;
89            }
90        }
91
92        foreach ($facts1 as $id1 => $fact1) {
93            foreach ($facts2 as $id2 => $fact2) {
94                if ($fact1->id() === $fact2->id()) {
95                    $facts[] = $fact1;
96                    unset($facts1[$id1], $facts2[$id2]);
97                }
98            }
99        }
100
101        return $this->viewResponse('admin/merge-records-step-2', [
102            'facts'   => $facts,
103            'facts1'  => $facts1,
104            'facts2'  => $facts2,
105            'record1' => $record1,
106            'record2' => $record2,
107            'title'   => $title,
108            'tree'    => $tree,
109        ]);
110    }
111}
112