1ce42304aSGreg Roach<?php 2ce42304aSGreg Roach 3ce42304aSGreg Roach/** 4ce42304aSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify 7ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by 8ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9ce42304aSGreg Roach * (at your option) any later version. 10ce42304aSGreg Roach * This program is distributed in the hope that it will be useful, 11ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13ce42304aSGreg Roach * GNU General Public License for more details. 14ce42304aSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16ce42304aSGreg Roach */ 17ce42304aSGreg Roach 18ce42304aSGreg Roachdeclare(strict_types=1); 19ce42304aSGreg Roach 20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Module; 21ce42304aSGreg Roach 22ce42304aSGreg Roachuse Fisharebest\Webtrees\Fact; 23ce42304aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 24ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N; 25ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\DataFixService; 26ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree; 27ce42304aSGreg Roachuse Illuminate\Database\Query\Builder; 28ce42304aSGreg Roachuse Illuminate\Support\Collection; 29ce42304aSGreg Roach 30ce42304aSGreg Roachuse function preg_match; 31dec352c1SGreg Roachuse function str_contains; 32ce42304aSGreg Roach 33ce42304aSGreg Roach/** 34ce42304aSGreg Roach * Class FixCemeteryTag 35ce42304aSGreg Roach */ 36ce42304aSGreg Roachclass FixCemeteryTag extends AbstractModule implements ModuleDataFixInterface 37ce42304aSGreg Roach{ 38ce42304aSGreg Roach use ModuleDataFixTrait; 39ce42304aSGreg Roach 4043f2f523SGreg Roach private DataFixService $data_fix_service; 41ce42304aSGreg Roach 42ce42304aSGreg Roach /** 43ce42304aSGreg Roach * @param DataFixService $data_fix_service 44ce42304aSGreg Roach */ 45ce42304aSGreg Roach public function __construct(DataFixService $data_fix_service) 46ce42304aSGreg Roach { 47ce42304aSGreg Roach $this->data_fix_service = $data_fix_service; 48ce42304aSGreg Roach } 49ce42304aSGreg Roach 50ce42304aSGreg Roach /** 51ce42304aSGreg Roach * How should this module be identified in the control panel, etc.? 52ce42304aSGreg Roach * 53ce42304aSGreg Roach * @return string 54ce42304aSGreg Roach */ 55ce42304aSGreg Roach public function title(): string 56ce42304aSGreg Roach { 57ce42304aSGreg Roach /* I18N: Name of a module */ 58e93aa0bdSGreg Roach return I18N::translate('Convert %s tags to GEDCOM 5.5.1', 'INDI:BURI:CEME'); 59ce42304aSGreg Roach } 60ce42304aSGreg Roach 61ce42304aSGreg Roach public function description(): string 62ce42304aSGreg Roach { 63ce42304aSGreg Roach /* I18N: Description of a “Data fix” module */ 64ce42304aSGreg Roach return I18N::translate('Replace cemetery tags with burial places.'); 65ce42304aSGreg Roach } 66ce42304aSGreg Roach 67ce42304aSGreg Roach /** 6876937c18SGreg Roach * Options form. 6976937c18SGreg Roach * 7076937c18SGreg Roach * @param Tree $tree 7176937c18SGreg Roach * 7276937c18SGreg Roach * @return string 7376937c18SGreg Roach */ 7476937c18SGreg Roach public function fixOptions(Tree $tree): string 7576937c18SGreg Roach { 7676937c18SGreg Roach $options = [ 7776937c18SGreg Roach 'ADDR' => I18N::translate('Address'), 7876937c18SGreg Roach 'PLAC' => I18N::translate('Place'), 7976937c18SGreg Roach ]; 8076937c18SGreg Roach 8176937c18SGreg Roach $selected = 'ADDR'; 8276937c18SGreg Roach 8376937c18SGreg Roach return view('modules/fix-ceme-tag/options', [ 8476937c18SGreg Roach 'options' => $options, 8576937c18SGreg Roach 'selected' => $selected, 8676937c18SGreg Roach ]); 8776937c18SGreg Roach } 8876937c18SGreg Roach 8976937c18SGreg Roach /** 90ce42304aSGreg Roach * A list of all records that need examining. This may include records 91ce42304aSGreg Roach * that do not need updating, if we can't detect this quickly using SQL. 92ce42304aSGreg Roach * 93ce42304aSGreg Roach * @param Tree $tree 94ce42304aSGreg Roach * @param array<string,string> $params 95ce42304aSGreg Roach * 9636779af1SGreg Roach * @return Collection<int,string>|null 97ce42304aSGreg Roach */ 98*1ff45046SGreg Roach protected function individualsToFix(Tree $tree, array $params): Collection|null 99ce42304aSGreg Roach { 1007684867eSGreg Roach return $this->individualsToFixQuery($tree, $params) 101ce42304aSGreg Roach ->where(static function (Builder $query): void { 102ce42304aSGreg Roach $query 103ce42304aSGreg Roach ->where('i_gedcom', 'LIKE', "%\n2 CEME%") 104ce42304aSGreg Roach ->orWhere('i_gedcom', 'LIKE', "%\n3 CEME%"); 105ce42304aSGreg Roach }) 106ce42304aSGreg Roach ->pluck('i_id'); 107ce42304aSGreg Roach } 108ce42304aSGreg Roach 109ce42304aSGreg Roach /** 110ce42304aSGreg Roach * Does a record need updating? 111ce42304aSGreg Roach * 112ce42304aSGreg Roach * @param GedcomRecord $record 113ce42304aSGreg Roach * @param array<string,string> $params 114ce42304aSGreg Roach * 115ce42304aSGreg Roach * @return bool 116ce42304aSGreg Roach */ 117ce42304aSGreg Roach public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool 118ce42304aSGreg Roach { 119ef6df466SGreg Roach return $record->facts(['BURI'], false, null, true) 120f25fc0f9SGreg Roach ->filter(static fn (Fact $fact): bool => preg_match('/\n[23] CEME/', $fact->gedcom()) === 1) 121ce42304aSGreg Roach ->isNotEmpty(); 122ce42304aSGreg Roach } 123ce42304aSGreg Roach 124ce42304aSGreg Roach /** 125ce42304aSGreg Roach * Show the changes we would make 126ce42304aSGreg Roach * 127ce42304aSGreg Roach * @param GedcomRecord $record 128ce42304aSGreg Roach * @param array<string,string> $params 129ce42304aSGreg Roach * 130ce42304aSGreg Roach * @return string 131ce42304aSGreg Roach */ 132ce42304aSGreg Roach public function previewUpdate(GedcomRecord $record, array $params): string 133ce42304aSGreg Roach { 13476937c18SGreg Roach $old = []; 13576937c18SGreg Roach $new = []; 13676937c18SGreg Roach 137ef6df466SGreg Roach foreach ($record->facts(['BURI'], false, null, true) as $fact) { 13876937c18SGreg Roach $old[] = $fact->gedcom(); 13976937c18SGreg Roach $new[] = $this->updateGedcom($fact, $params); 14076937c18SGreg Roach } 14176937c18SGreg Roach 14276937c18SGreg Roach $old = implode("\n", $old); 14376937c18SGreg Roach $new = implode("\n", $new); 144ce42304aSGreg Roach 145ce42304aSGreg Roach return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); 146ce42304aSGreg Roach } 147ce42304aSGreg Roach 148ce42304aSGreg Roach /** 149ce42304aSGreg Roach * Fix a record 150ce42304aSGreg Roach * 151ce42304aSGreg Roach * @param GedcomRecord $record 152ce42304aSGreg Roach * @param array<string,string> $params 153ce42304aSGreg Roach * 154ce42304aSGreg Roach * @return void 155ce42304aSGreg Roach */ 156ce42304aSGreg Roach public function updateRecord(GedcomRecord $record, array $params): void 157ce42304aSGreg Roach { 158ef6df466SGreg Roach foreach ($record->facts(['BURI'], false, null, true) as $fact) { 15976937c18SGreg Roach $record->updateFact($fact->id(), $this->updateGedcom($fact, $params), false); 16076937c18SGreg Roach } 161ce42304aSGreg Roach } 162ce42304aSGreg Roach 163ce42304aSGreg Roach /** 16476937c18SGreg Roach * @param Fact $fact 16576937c18SGreg Roach * @param array<string,string> $params 166ce42304aSGreg Roach * 167ce42304aSGreg Roach * @return string 168ce42304aSGreg Roach */ 16976937c18SGreg Roach private function updateGedcom(Fact $fact, array $params): string 170ce42304aSGreg Roach { 17176937c18SGreg Roach $gedcom = $fact->gedcom(); 17276937c18SGreg Roach 17376937c18SGreg Roach if (preg_match('/\n\d CEME ?(.+)(?:\n\d PLOT ?(.+))?/', $gedcom, $match)) { 17476937c18SGreg Roach $ceme = $match[1]; 175ef6df466SGreg Roach $plot = $match[2] ?? ''; 17676937c18SGreg Roach 17776937c18SGreg Roach // Merge PLOT with CEME 17876937c18SGreg Roach if ($plot !== '') { 17976937c18SGreg Roach $ceme = $plot . ', ' . $ceme; 18076937c18SGreg Roach } 18176937c18SGreg Roach 18276937c18SGreg Roach // Remove CEME/PLOT 18376937c18SGreg Roach $gedcom = strtr($gedcom, [$match[0] => '']); 18476937c18SGreg Roach 18576937c18SGreg Roach // Add PLAC/ADDR 18676937c18SGreg Roach $convert = $params['convert']; 18776937c18SGreg Roach 188dec352c1SGreg Roach if (!str_contains($gedcom, "\n2 " . $convert . ' ')) { 18976937c18SGreg Roach $gedcom .= "\n2 " . $convert . ' ' . $ceme; 19076937c18SGreg Roach } else { 19176937c18SGreg Roach $gedcom = strtr($gedcom, ["\n2 " . $convert . ' ' => "\n2 " . $convert . ' ' . $ceme . ', ']); 19276937c18SGreg Roach } 19376937c18SGreg Roach } 19476937c18SGreg Roach 19576937c18SGreg Roach return $gedcom; 196ce42304aSGreg Roach } 197ce42304aSGreg Roach} 198