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