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\Module; 21 22use Fisharebest\Webtrees\Fact; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\DataFixService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Database\Capsule\Manager as DB; 28use Illuminate\Database\Query\Builder; 29use Illuminate\Support\Collection; 30 31use function preg_match; 32 33/** 34 * Class FixCemeteryTag 35 */ 36class FixCemeteryTag extends AbstractModule implements ModuleDataFixInterface 37{ 38 use ModuleDataFixTrait; 39 40 /** @var DataFixService */ 41 private $data_fix_service; 42 43 /** 44 * FixMissingDeaths constructor. 45 * 46 * @param DataFixService $data_fix_service 47 */ 48 public function __construct(DataFixService $data_fix_service) 49 { 50 $this->data_fix_service = $data_fix_service; 51 } 52 53 /** 54 * How should this module be identified in the control panel, etc.? 55 * 56 * @return string 57 */ 58 public function title(): string 59 { 60 /* I18N: Name of a module */ 61 return I18N::translate('Convert CEME tags to GEDCOM 5.5.1'); 62 } 63 64 /** 65 * A sentence describing what this module does. 66 * 67 * @return string 68 */ 69 public function description(): string 70 { 71 /* I18N: Description of a “Data fix” module */ 72 return I18N::translate('Replace cemetery tags with burial places.'); 73 } 74 75 /** 76 * Options form. 77 * 78 * @param Tree $tree 79 * 80 * @return string 81 */ 82 public function fixOptions(Tree $tree): string 83 { 84 $options = [ 85 'ADDR' => I18N::translate('Address'), 86 'PLAC' => I18N::translate('Place'), 87 ]; 88 89 $selected = 'ADDR'; 90 91 return view('modules/fix-ceme-tag/options', [ 92 'options' => $options, 93 'selected' => $selected, 94 ]); 95 } 96 97 /** 98 * A list of all records that need examining. This may include records 99 * that do not need updating, if we can't detect this quickly using SQL. 100 * 101 * @param Tree $tree 102 * @param array<string,string> $params 103 * 104 * @return Collection<string>|null 105 */ 106 protected function individualsToFix(Tree $tree, array $params): ?Collection 107 { 108 // No DB querying possible? Select all. 109 return DB::table('individuals') 110 ->where('i_file', '=', $tree->id()) 111 ->where(static function (Builder $query): void { 112 $query 113 ->where('i_gedcom', 'LIKE', "%\n2 CEME%") 114 ->orWhere('i_gedcom', 'LIKE', "%\n3 CEME%"); 115 }) 116 ->pluck('i_id'); 117 } 118 119 /** 120 * Does a record need updating? 121 * 122 * @param GedcomRecord $record 123 * @param array<string,string> $params 124 * 125 * @return bool 126 */ 127 public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool 128 { 129 return $record->facts(['BURI'], false, null, true) 130 ->filter(static function (Fact $fact): bool { 131 return preg_match('/\n[23] CEME/', $fact->gedcom()) === 1; 132 }) 133 ->isNotEmpty(); 134 } 135 136 /** 137 * Show the changes we would make 138 * 139 * @param GedcomRecord $record 140 * @param array<string,string> $params 141 * 142 * @return string 143 */ 144 public function previewUpdate(GedcomRecord $record, array $params): string 145 { 146 $old = []; 147 $new = []; 148 149 foreach ($record->facts(['BURI'], false, null, true) as $fact) { 150 $old[] = $fact->gedcom(); 151 $new[] = $this->updateGedcom($fact, $params); 152 } 153 154 $old = implode("\n", $old); 155 $new = implode("\n", $new); 156 157 return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); 158 } 159 160 /** 161 * Fix a record 162 * 163 * @param GedcomRecord $record 164 * @param array<string,string> $params 165 * 166 * @return void 167 */ 168 public function updateRecord(GedcomRecord $record, array $params): void 169 { 170 foreach ($record->facts(['BURI'], false, null, true) as $fact) { 171 $record->updateFact($fact->id(), $this->updateGedcom($fact, $params), false); 172 } 173 } 174 175 /** 176 * @param Fact $fact 177 * @param array<string,string> $params 178 * 179 * @return string 180 */ 181 private function updateGedcom(Fact $fact, array $params): string 182 { 183 $gedcom = $fact->gedcom(); 184 185 if (preg_match('/\n\d CEME ?(.+)(?:\n\d PLOT ?(.+))?/', $gedcom, $match)) { 186 $ceme = $match[1]; 187 $plot = $match[2] ?? ''; 188 189 // Merge PLOT with CEME 190 if ($plot !== '') { 191 $ceme = $plot . ', ' . $ceme; 192 } 193 194 // Remove CEME/PLOT 195 $gedcom = strtr($gedcom, [$match[0] => '']); 196 197 // Add PLAC/ADDR 198 $convert = $params['convert']; 199 200 if (strpos($gedcom, "\n2 " . $convert . ' ') === false) { 201 $gedcom .= "\n2 " . $convert . ' ' . $ceme; 202 } else { 203 $gedcom = strtr($gedcom, ["\n2 " . $convert . ' ' => "\n2 " . $convert . ' ' . $ceme . ', ']); 204 } 205 } 206 207 return $gedcom; 208 } 209} 210