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