xref: /webtrees/app/Http/RequestHandlers/MergeFactsAction.php (revision 5bc5bf805eeeaf271674e6a1ea666230dcb73c3d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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\Factory;
24use Fisharebest\Webtrees\FlashMessages;
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 = Factory::gedcomRecord()->make($xref1, $tree);
67        $record2 = Factory::gedcomRecord()->make($xref2, $tree);
68
69        if (
70            $record1 === null ||
71            $record2 === null ||
72            $record1 === $record2 ||
73            $record1->tag() !== $record2->tag() ||
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                ->where('page_parameter', '=', $xref1)
131                ->update(['page_count' => $page_count]);
132        }
133
134        DB::table('hit_counter')
135            ->where('gedcom_id', '=', $tree->id())
136            ->where('page_parameter', '=', $xref2)
137            ->delete();
138
139        $gedcom = '0 @' . $record1->xref() . '@ ' . $record1->tag();
140
141        foreach ($record1->facts() as $fact) {
142            if (in_array($fact->id(), $keep1, true)) {
143                $gedcom .= "\n" . $fact->gedcom();
144            }
145        }
146
147        foreach ($record2->facts() as $fact) {
148            if (in_array($fact->id(), $keep2, true)) {
149                $gedcom .= "\n" . $fact->gedcom();
150            }
151        }
152
153        DB::table('favorite')
154            ->where('gedcom_id', '=', $tree->id())
155            ->where('xref', '=', $xref2)
156            ->update(['xref' => $xref1]);
157
158        $record1->updateRecord($gedcom, true);
159        $record2->deleteRecord();
160
161        /* I18N: Records are individuals, sources, etc. */
162        FlashMessages::addMessage(I18N::translate(
163            'The records “%1$s” and “%2$s” have been merged.',
164            '<a class="alert-link" href="' . e($record1->url()) . '">' . $record1->fullName() . '</a>',
165            $record2_name
166        ), 'success');
167
168        return redirect(route(MergeRecordsPage::class, ['tree' => $tree->name()]));
169    }
170}
171