xref: /webtrees/app/Module/FixCemeteryTag.php (revision 31157258addeb527509e12b5b145cccb0b110e40)
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
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 function (Fact $fact): bool {
126                return preg_match('/\n[23] CEME/', $fact->gedcom()) === 1;
127            })
128            ->isNotEmpty();
129    }
130
131    /**
132     * Show the changes we would make
133     *
134     * @param GedcomRecord         $record
135     * @param array<string,string> $params
136     *
137     * @return string
138     */
139    public function previewUpdate(GedcomRecord $record, array $params): string
140    {
141        $old = [];
142        $new = [];
143
144        foreach ($record->facts(['BURI'], false, null, true) as $fact) {
145            $old[] = $fact->gedcom();
146            $new[] = $this->updateGedcom($fact, $params);
147        }
148
149        $old = implode("\n", $old);
150        $new = implode("\n", $new);
151
152        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
153    }
154
155    /**
156     * Fix a record
157     *
158     * @param GedcomRecord         $record
159     * @param array<string,string> $params
160     *
161     * @return void
162     */
163    public function updateRecord(GedcomRecord $record, array $params): void
164    {
165        foreach ($record->facts(['BURI'], false, null, true) as $fact) {
166            $record->updateFact($fact->id(), $this->updateGedcom($fact, $params), false);
167        }
168    }
169
170    /**
171     * @param Fact                 $fact
172     * @param array<string,string> $params
173     *
174     * @return string
175     */
176    private function updateGedcom(Fact $fact, array $params): string
177    {
178        $gedcom = $fact->gedcom();
179
180        if (preg_match('/\n\d CEME ?(.+)(?:\n\d PLOT ?(.+))?/', $gedcom, $match)) {
181            $ceme = $match[1];
182            $plot = $match[2] ?? '';
183
184            // Merge PLOT with CEME
185            if ($plot !== '') {
186                $ceme = $plot . ', ' . $ceme;
187            }
188
189            // Remove CEME/PLOT
190            $gedcom = strtr($gedcom, [$match[0] => '']);
191
192            // Add PLAC/ADDR
193            $convert = $params['convert'];
194
195            if (!str_contains($gedcom, "\n2 " . $convert . ' ')) {
196                $gedcom .= "\n2 " . $convert . ' ' . $ceme;
197            } else {
198                $gedcom = strtr($gedcom, ["\n2 " . $convert . ' ' => "\n2 " . $convert . ' ' . $ceme . ', ']);
199            }
200        }
201
202        return $gedcom;
203    }
204}
205