xref: /webtrees/app/Module/FixMissingDeaths.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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    /**
59     * A sentence describing what this module does.
60     *
61     * @return string
62     */
63    public function description(): string
64    {
65        /* I18N: Description of a “Data fix” module */
66        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.');
67    }
68
69    /**
70     * A list of all records that need examining.  This may include records
71     * that do not need updating, if we can't detect this quickly using SQL.
72     *
73     * @param Tree                 $tree
74     * @param array<string,string> $params
75     *
76     * @return Collection<int,string>|null
77     */
78    protected function individualsToFix(Tree $tree, array $params): Collection|null
79    {
80        $query = $this->individualsToFixQuery($tree, $params);
81
82        foreach (Gedcom::DEATH_EVENTS as $event) {
83            $query->where('i_gedcom', 'NOT LIKE', "%\n1 " . $event . '%');
84        }
85
86        return $query->pluck('i_id');
87    }
88
89    /**
90     * Does a record need updating?
91     *
92     * @param GedcomRecord         $record
93     * @param array<string,string> $params
94     *
95     * @return bool
96     */
97    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
98    {
99        return
100            $record instanceof Individual &&
101            $record->facts(Gedcom::DEATH_EVENTS, false, null, true)->isEmpty() &&
102            $record->isDead();
103    }
104
105    /**
106     * Show the changes we would make
107     *
108     * @param GedcomRecord         $record
109     * @param array<string,string> $params
110     *
111     * @return string
112     */
113    public function previewUpdate(GedcomRecord $record, array $params): string
114    {
115        $old = $record->gedcom();
116        $new = $this->updateGedcom($record);
117
118        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
119    }
120
121    /**
122     * Fix a record
123     *
124     * @param GedcomRecord         $record
125     * @param array<string,string> $params
126     *
127     * @return void
128     */
129    public function updateRecord(GedcomRecord $record, array $params): void
130    {
131        $record->updateRecord($this->updateGedcom($record), false);
132    }
133
134    /**
135     * @param GedcomRecord $record
136     *
137     * @return string
138     */
139    private function updateGedcom(GedcomRecord $record): string
140    {
141        return $record->gedcom() . "\n1 DEAT Y";
142    }
143}
144