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