xref: /webtrees/app/Http/RequestHandlers/MergeFactsAction.php (revision ba1d7494e9648df5a87aaa3fece3e098e5ce26b1)
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\Auth;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\User;
28use Illuminate\Database\Capsule\Manager as DB;
29use Illuminate\Database\Query\Expression;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function assert;
35use function e;
36use function in_array;
37use function preg_replace;
38use function redirect;
39use function route;
40use function str_replace;
41
42/**
43 * Merge records
44 */
45class MergeFactsAction implements RequestHandlerInterface
46{
47    /**
48     * @param ServerRequestInterface $request
49     *
50     * @return ResponseInterface
51     */
52    public function handle(ServerRequestInterface $request): ResponseInterface
53    {
54        $tree = $request->getAttribute('tree');
55        assert($tree instanceof Tree);
56
57        $xref1 = $request->getParsedBody()['xref1'] ?? '';
58        $xref2 = $request->getParsedBody()['xref2'] ?? '';
59
60        $keep1 = $request->getParsedBody()['keep1'] ?? [];
61        $keep2 = $request->getParsedBody()['keep2'] ?? [];
62
63        // Merge record2 into record1
64        $record1 = GedcomRecord::getInstance($xref1, $tree);
65        $record2 = GedcomRecord::getInstance($xref2, $tree);
66
67        if (
68            $record1 === null ||
69            $record2 === null ||
70            $record1 === $record2 ||
71            $record1::RECORD_TYPE !== $record2::RECORD_TYPE ||
72            $record1->isPendingDeletion() ||
73            $record2->isPendingDeletion()
74        ) {
75            return redirect(route(MergeRecordsPage::class, [
76                'tree'  => $tree->name(),
77                'xref1' => $xref1,
78                'xref2' => $xref2,
79            ]));
80        }
81
82        // If we are not auto-accepting, then we can show a link to the pending deletion
83        if (Auth::user()->getPreference(User::PREF_AUTO_ACCEPT_EDITS) === '1') {
84            $record2_name = $record2->fullName();
85        } else {
86            $record2_name = '<a class="alert-link" href="' . e($record2->url()) . '">' . $record2->fullName() . '</a>';
87        }
88
89        // Update records that link to the one we will be removing.
90        $linking_records = $record2->linkingRecords();
91
92        foreach ($linking_records as $record) {
93            if (!$record->isPendingDeletion()) {
94                /* I18N: The placeholders are the names of individuals, sources, etc. */
95                FlashMessages::addMessage(I18N::translate(
96                    'The link from “%1$s” to “%2$s” has been updated.',
97                    '<a class="alert-link" href="' . e($record->url()) . '">' . $record->fullName() . '</a>',
98                    $record2_name
99                ), 'info');
100                $gedcom = str_replace('@' . $xref2 . '@', '@' . $xref1 . '@', $record->gedcom());
101                $gedcom = preg_replace(
102                    '/(\n1.*@.+@.*(?:(?:\n[2-9].*)*))((?:\n1.*(?:\n[2-9].*)*)*\1)/',
103                    '$2',
104                    $gedcom
105                );
106                $record->updateRecord($gedcom, true);
107            }
108        }
109
110        // Update any linked user-accounts
111        DB::table('user_gedcom_setting')
112            ->where('gedcom_id', '=', $tree->id())
113            ->whereIn('setting_name', [User::PREF_TREE_ACCOUNT_XREF, User::PREF_TREE_DEFAULT_XREF])
114            ->where('setting_value', '=', $xref2)
115            ->update(['setting_value' => $xref1]);
116
117        // Merge hit counters
118        $hits = DB::table('hit_counter')
119            ->where('gedcom_id', '=', $tree->id())
120            ->whereIn('page_parameter', [$xref1, $xref2])
121            ->groupBy(['page_name'])
122            ->pluck(new Expression('SUM(page_count)'), 'page_name');
123
124        foreach ($hits as $page_name => $page_count) {
125            DB::table('hit_counter')
126                ->where('gedcom_id', '=', $tree->id())
127                ->where('page_name', '=', $page_name)
128                ->update(['page_count' => $page_count]);
129        }
130
131        DB::table('hit_counter')
132            ->where('gedcom_id', '=', $tree->id())
133            ->where('page_parameter', '=', $xref2)
134            ->delete();
135
136        $gedcom = '0 @' . $record1->xref() . '@ ' . $record1::RECORD_TYPE;
137
138        foreach ($record1->facts() as $fact) {
139            if (in_array($fact->id(), $keep1, true)) {
140                $gedcom .= "\n" . $fact->gedcom();
141            }
142        }
143
144        foreach ($record2->facts() as $fact) {
145            if (in_array($fact->id(), $keep2, true)) {
146                $gedcom .= "\n" . $fact->gedcom();
147            }
148        }
149
150        DB::table('favorite')
151            ->where('gedcom_id', '=', $tree->id())
152            ->where('xref', '=', $xref2)
153            ->update(['xref' => $xref1]);
154
155        $record1->updateRecord($gedcom, true);
156        $record2->deleteRecord();
157
158        /* I18N: Records are individuals, sources, etc. */
159        FlashMessages::addMessage(I18N::translate(
160            'The records “%1$s” and “%2$s” have been merged.',
161            '<a class="alert-link" href="' . e($record1->url()) . '">' . $record1->fullName() . '</a>',
162            $record2_name
163        ), 'success');
164
165        return redirect(route(MergeRecordsPage::class, ['tree' => $tree->name()]));
166    }
167}
168