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