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