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