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