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