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