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