xref: /webtrees/app/Module/FixMissingDeaths.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
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\Gedcom;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Services\DataFixService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29
30/**
31 * Class FixMissingDeaths
32 */
33class FixMissingDeaths extends AbstractModule implements ModuleDataFixInterface
34{
35    use ModuleDataFixTrait;
36
37    private DataFixService $data_fix_service;
38
39    /**
40     * @param DataFixService $data_fix_service
41     */
42    public function __construct(DataFixService $data_fix_service)
43    {
44        $this->data_fix_service = $data_fix_service;
45    }
46
47    /**
48     * How should this module be identified in the control panel, etc.?
49     *
50     * @return string
51     */
52    public function title(): string
53    {
54        /* I18N: Name of a module */
55        return I18N::translate('Add missing death records');
56    }
57
58    public function description(): string
59    {
60        /* I18N: Description of a “Data fix” module */
61        return I18N::translate('You can speed up the privacy calculations by adding a death record to individuals whose death can be inferred from other dates, but who do not have a record of death, burial, cremation, etc.');
62    }
63
64    /**
65     * A list of all records that need examining.  This may include records
66     * that do not need updating, if we can't detect this quickly using SQL.
67     *
68     * @param Tree                 $tree
69     * @param array<string,string> $params
70     *
71     * @return Collection<int,string>|null
72     */
73    protected function individualsToFix(Tree $tree, array $params): Collection|null
74    {
75        $query = $this->individualsToFixQuery($tree, $params);
76
77        foreach (Gedcom::DEATH_EVENTS as $event) {
78            $query->where('i_gedcom', 'NOT LIKE', "%\n1 " . $event . '%');
79        }
80
81        return $query->pluck('i_id');
82    }
83
84    /**
85     * Does a record need updating?
86     *
87     * @param GedcomRecord         $record
88     * @param array<string,string> $params
89     *
90     * @return bool
91     */
92    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
93    {
94        return
95            $record instanceof Individual &&
96            $record->facts(Gedcom::DEATH_EVENTS, false, null, true)->isEmpty() &&
97            $record->isDead();
98    }
99
100    /**
101     * Show the changes we would make
102     *
103     * @param GedcomRecord         $record
104     * @param array<string,string> $params
105     *
106     * @return string
107     */
108    public function previewUpdate(GedcomRecord $record, array $params): string
109    {
110        $old = $record->gedcom();
111        $new = $this->updateGedcom($record);
112
113        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
114    }
115
116    /**
117     * Fix a record
118     *
119     * @param GedcomRecord         $record
120     * @param array<string,string> $params
121     *
122     * @return void
123     */
124    public function updateRecord(GedcomRecord $record, array $params): void
125    {
126        $record->updateRecord($this->updateGedcom($record), false);
127    }
128
129    /**
130     * @param GedcomRecord $record
131     *
132     * @return string
133     */
134    private function updateGedcom(GedcomRecord $record): string
135    {
136        return $record->gedcom() . "\n1 DEAT Y";
137    }
138}
139