|
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
|
|
|
|
|
18
|
|
|
declare(strict_types=1); |
|
19
|
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Http\RequestHandlers; |
|
21
|
|
|
|
|
22
|
|
|
use Fisharebest\Webtrees\Family; |
|
23
|
|
|
use Fisharebest\Webtrees\FlashMessages; |
|
24
|
|
|
use Fisharebest\Webtrees\GedcomRecord; |
|
25
|
|
|
use Fisharebest\Webtrees\I18N; |
|
26
|
|
|
use Fisharebest\Webtrees\Individual; |
|
27
|
|
|
use Fisharebest\Webtrees\Tree; |
|
28
|
|
|
use Illuminate\Database\Capsule\Manager as DB; |
|
29
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
30
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
31
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
32
|
|
|
|
|
33
|
|
|
use function assert; |
|
34
|
|
|
use function e; |
|
35
|
|
|
use function preg_match; |
|
36
|
|
|
use function preg_quote; |
|
37
|
|
|
use function preg_replace; |
|
38
|
|
|
use function redirect; |
|
39
|
|
|
use function route; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Update place names. |
|
43
|
|
|
*/ |
|
44
|
|
|
class UpdatePlacesAction implements RequestHandlerInterface |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @param ServerRequestInterface $request |
|
48
|
|
|
* |
|
49
|
|
|
* @return ResponseInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$tree = $request->getAttribute('tree'); |
|
54
|
|
|
assert($tree instanceof Tree); |
|
55
|
|
|
|
|
56
|
|
|
$replace = $request->getParsedBody()['replace'] ?? ''; |
|
57
|
|
|
$search = $request->getParsedBody()['search'] ?? ''; |
|
58
|
|
|
$submit = $request->getParsedBody()['submit'] ?? ''; |
|
59
|
|
|
|
|
60
|
|
|
if ($search !== '' && $replace !== '' && $submit === 'update') { |
|
61
|
|
|
$individual_changes = DB::table('individuals') |
|
62
|
|
|
->where('i_file', '=', $tree->id()) |
|
63
|
|
|
->whereContains('i_gedcom', $search) |
|
64
|
|
|
->select(['individuals.*']) |
|
65
|
|
|
->get() |
|
66
|
|
|
->map(Individual::rowMapper()); |
|
67
|
|
|
|
|
68
|
|
|
$family_changes = DB::table('families') |
|
69
|
|
|
->where('f_file', '=', $tree->id()) |
|
70
|
|
|
->whereContains('f_gedcom', $search) |
|
71
|
|
|
->select(['families.*']) |
|
72
|
|
|
->get() |
|
73
|
|
|
->map(Family::rowMapper()); |
|
74
|
|
|
|
|
75
|
|
|
$changes = $individual_changes->merge($family_changes) |
|
76
|
|
|
->mapWithKeys(static function (GedcomRecord $record) use ($search, $replace): array { |
|
77
|
|
|
$changes = []; |
|
78
|
|
|
|
|
79
|
|
|
foreach ($record->facts() as $fact) { |
|
80
|
|
|
$old_place = $fact->attribute('PLAC'); |
|
81
|
|
|
if (preg_match('/(^|, )' . preg_quote($search, '/') . '$/i', $old_place)) { |
|
82
|
|
|
$new_place = preg_replace('/(^|, )' . preg_quote($search, '/') . '$/i', '$1' . $replace, $old_place); |
|
83
|
|
|
$changes[$old_place] = $new_place; |
|
84
|
|
|
$gedcom = preg_replace('/(\n2 PLAC (?:.*, )*)' . preg_quote($search, '/') . '(\n|$)/i', '$1' . $replace . '$2', $fact->gedcom()); |
|
85
|
|
|
$record->updateFact($fact->id(), $gedcom, false); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $changes; |
|
90
|
|
|
}) |
|
91
|
|
|
->sort(); |
|
92
|
|
|
|
|
93
|
|
|
$feedback = I18N::translate('The following places have been changed:') . '<ul>'; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($changes as $old_place => $new_place) { |
|
96
|
|
|
$feedback .= '<li>' . e($old_place) . ' → ' . e($new_place) . '</li>'; |
|
97
|
|
|
} |
|
98
|
|
|
$feedback .= '</ul>'; |
|
99
|
|
|
|
|
100
|
|
|
FlashMessages::addMessage($feedback, 'success'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return redirect(route(UpdatePlacesPage::class, [ |
|
104
|
|
|
'tree' => $tree->name(), |
|
105
|
|
|
'replace' => $replace, |
|
106
|
|
|
'search' => $search, |
|
107
|
|
|
])); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|