xref: /webtrees/app/Http/RequestHandlers/MergeFactsAction.php (revision 8f8787974040d069eb8daff5e2b4af725c6bd747)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Auth;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Fisharebest\Webtrees\FlashMessages;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Validator;
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 e;
35use function in_array;
36use function preg_replace;
37use function redirect;
38use function route;
39use function str_replace;
40
41/**
42 * Merge records
43 */
44class MergeFactsAction implements RequestHandlerInterface
45{
46    /**
47     * @param ServerRequestInterface $request
48     *
49     * @return ResponseInterface
50     */
51    public function handle(ServerRequestInterface $request): ResponseInterface
52    {
53        $tree = Validator::attributes($request)->tree();
54
55        $params = (array) $request->getParsedBody();
56
57        $xref1 = $params['xref1'] ?? '';
58        $xref2 = $params['xref2'] ?? '';
59
60        $keep1 = $params['keep1'] ?? [];
61        $keep2 = $params['keep2'] ?? [];
62
63        // Merge record2 into record1
64        $record1 = Registry::gedcomRecordFactory()->make($xref1, $tree);
65        $record2 = Registry::gedcomRecordFactory()->make($xref2, $tree);
66
67        if (
68            $record1 === null ||
69            $record2 === null ||
70            $record1 === $record2 ||
71            $record1->tag() !== $record2->tag() ||
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(UserInterface::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', [UserInterface::PREF_TREE_ACCOUNT_XREF, UserInterface::PREF_TREE_DEFAULT_XREF])
114            ->where('setting_value', '=', $xref2)
115            ->update(['setting_value' => $xref1]);
116
117        // Merge stories, etc.
118        DB::table('block')
119            ->where('gedcom_id', '=', $tree->id())
120            ->where('xref', '=', $xref2)
121            ->update(['xref' => $xref1]);
122
123        // Merge hit counters
124        $hits = DB::table('hit_counter')
125            ->where('gedcom_id', '=', $tree->id())
126            ->whereIn('page_parameter', [$xref1, $xref2])
127            ->groupBy(['page_name'])
128            ->pluck(new Expression('SUM(page_count)'), 'page_name');
129
130        foreach ($hits as $page_name => $page_count) {
131            DB::table('hit_counter')
132                ->where('gedcom_id', '=', $tree->id())
133                ->where('page_name', '=', $page_name)
134                ->where('page_parameter', '=', $xref1)
135                ->update(['page_count' => $page_count]);
136        }
137
138        DB::table('hit_counter')
139            ->where('gedcom_id', '=', $tree->id())
140            ->where('page_parameter', '=', $xref2)
141            ->delete();
142
143        $gedcom = '0 @' . $record1->xref() . '@ ' . $record1->tag();
144
145        foreach ($record1->facts() as $fact) {
146            if (in_array($fact->id(), $keep1, true)) {
147                $gedcom .= "\n" . $fact->gedcom();
148            }
149        }
150
151        foreach ($record2->facts() as $fact) {
152            if (in_array($fact->id(), $keep2, true)) {
153                $gedcom .= "\n" . $fact->gedcom();
154            }
155        }
156
157        DB::table('favorite')
158            ->where('gedcom_id', '=', $tree->id())
159            ->where('xref', '=', $xref2)
160            ->update(['xref' => $xref1]);
161
162        $record1->updateRecord($gedcom, true);
163        $record2->deleteRecord();
164
165        /* I18N: Records are individuals, sources, etc. */
166        FlashMessages::addMessage(I18N::translate(
167            'The records “%1$s” and “%2$s” have been merged.',
168            '<a class="alert-link" href="' . e($record1->url()) . '">' . $record1->fullName() . '</a>',
169            $record2_name
170        ), 'success');
171
172        return redirect(route(ManageTrees::class, ['tree' => $tree->name()]));
173    }
174}
175