xref: /webtrees/app/Module/FixMissingDeaths.php (revision d11be7027e34e3121be11cc025421873364403f9)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify
7ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by
8ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ce42304aSGreg Roach * (at your option) any later version.
10ce42304aSGreg Roach * This program is distributed in the hope that it will be useful,
11ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ce42304aSGreg Roach * GNU General Public License for more details.
14ce42304aSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ce42304aSGreg Roach */
17ce42304aSGreg Roach
18ce42304aSGreg Roachdeclare(strict_types=1);
19ce42304aSGreg Roach
20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Module;
21ce42304aSGreg Roach
22ce42304aSGreg Roachuse Fisharebest\Webtrees\Gedcom;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
24ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
25ce42304aSGreg Roachuse Fisharebest\Webtrees\Individual;
26ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\DataFixService;
27ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree;
28ce42304aSGreg Roachuse Illuminate\Support\Collection;
29ce42304aSGreg Roach
30ce42304aSGreg Roach/**
31ce42304aSGreg Roach * Class FixMissingDeaths
32ce42304aSGreg Roach */
33ce42304aSGreg Roachclass FixMissingDeaths extends AbstractModule implements ModuleDataFixInterface
34ce42304aSGreg Roach{
35ce42304aSGreg Roach    use ModuleDataFixTrait;
36ce42304aSGreg Roach
3743f2f523SGreg Roach    private DataFixService $data_fix_service;
38ce42304aSGreg Roach
39ce42304aSGreg Roach    /**
40ce42304aSGreg Roach     * FixMissingDeaths constructor.
41ce42304aSGreg Roach     *
42ce42304aSGreg Roach     * @param DataFixService $data_fix_service
43ce42304aSGreg Roach     */
44ce42304aSGreg Roach    public function __construct(DataFixService $data_fix_service)
45ce42304aSGreg Roach    {
46ce42304aSGreg Roach        $this->data_fix_service = $data_fix_service;
47ce42304aSGreg Roach    }
48ce42304aSGreg Roach
49ce42304aSGreg Roach    /**
50ce42304aSGreg Roach     * How should this module be identified in the control panel, etc.?
51ce42304aSGreg Roach     *
52ce42304aSGreg Roach     * @return string
53ce42304aSGreg Roach     */
54ce42304aSGreg Roach    public function title(): string
55ce42304aSGreg Roach    {
56ce42304aSGreg Roach        /* I18N: Name of a module */
57ce42304aSGreg Roach        return I18N::translate('Add missing death records');
58ce42304aSGreg Roach    }
59ce42304aSGreg Roach
60ce42304aSGreg Roach    /**
61ce42304aSGreg Roach     * A sentence describing what this module does.
62ce42304aSGreg Roach     *
63ce42304aSGreg Roach     * @return string
64ce42304aSGreg Roach     */
65ce42304aSGreg Roach    public function description(): string
66ce42304aSGreg Roach    {
67ce42304aSGreg Roach        /* I18N: Description of a “Data fix” module */
68ce42304aSGreg Roach        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.');
69ce42304aSGreg Roach    }
70ce42304aSGreg Roach
71ce42304aSGreg Roach    /**
72ce42304aSGreg Roach     * A list of all records that need examining.  This may include records
73ce42304aSGreg Roach     * that do not need updating, if we can't detect this quickly using SQL.
74ce42304aSGreg Roach     *
75ce42304aSGreg Roach     * @param Tree                 $tree
76ce42304aSGreg Roach     * @param array<string,string> $params
77ce42304aSGreg Roach     *
7836779af1SGreg Roach     * @return Collection<int,string>|null
79ce42304aSGreg Roach     */
80ce42304aSGreg Roach    protected function individualsToFix(Tree $tree, array $params): ?Collection
81ce42304aSGreg Roach    {
827684867eSGreg Roach        $query = $this->individualsToFixQuery($tree, $params);
83ce42304aSGreg Roach
84ce42304aSGreg Roach        foreach (Gedcom::DEATH_EVENTS as $event) {
85ce42304aSGreg Roach            $query->where('i_gedcom', 'NOT LIKE', "%\n1 " . $event . '%');
86ce42304aSGreg Roach        }
87ce42304aSGreg Roach
88ce42304aSGreg Roach        return $query->pluck('i_id');
89ce42304aSGreg Roach    }
90ce42304aSGreg Roach
91ce42304aSGreg Roach    /**
92ce42304aSGreg Roach     * Does a record need updating?
93ce42304aSGreg Roach     *
94ce42304aSGreg Roach     * @param GedcomRecord         $record
95ce42304aSGreg Roach     * @param array<string,string> $params
96ce42304aSGreg Roach     *
97ce42304aSGreg Roach     * @return bool
98ce42304aSGreg Roach     */
99ce42304aSGreg Roach    public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool
100ce42304aSGreg Roach    {
101ce42304aSGreg Roach        return
102ce42304aSGreg Roach            $record instanceof Individual &&
103ce42304aSGreg Roach            $record->facts(Gedcom::DEATH_EVENTS, false, null, true)->isEmpty() &&
104ce42304aSGreg Roach            $record->isDead();
105ce42304aSGreg Roach    }
106ce42304aSGreg Roach
107ce42304aSGreg Roach    /**
108ce42304aSGreg Roach     * Show the changes we would make
109ce42304aSGreg Roach     *
110ce42304aSGreg Roach     * @param GedcomRecord         $record
111ce42304aSGreg Roach     * @param array<string,string> $params
112ce42304aSGreg Roach     *
113ce42304aSGreg Roach     * @return string
114ce42304aSGreg Roach     */
115ce42304aSGreg Roach    public function previewUpdate(GedcomRecord $record, array $params): string
116ce42304aSGreg Roach    {
117ce42304aSGreg Roach        $old = $record->gedcom();
118ce42304aSGreg Roach        $new = $this->updateGedcom($record);
119ce42304aSGreg Roach
120ce42304aSGreg Roach        return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);
121ce42304aSGreg Roach    }
122ce42304aSGreg Roach
123ce42304aSGreg Roach    /**
124ce42304aSGreg Roach     * Fix a record
125ce42304aSGreg Roach     *
126ce42304aSGreg Roach     * @param GedcomRecord         $record
127ce42304aSGreg Roach     * @param array<string,string> $params
128ce42304aSGreg Roach     *
129ce42304aSGreg Roach     * @return void
130ce42304aSGreg Roach     */
131ce42304aSGreg Roach    public function updateRecord(GedcomRecord $record, array $params): void
132ce42304aSGreg Roach    {
133ce42304aSGreg Roach        $record->updateRecord($this->updateGedcom($record), false);
134ce42304aSGreg Roach    }
135ce42304aSGreg Roach
136ce42304aSGreg Roach    /**
137ce42304aSGreg Roach     * @param GedcomRecord $record
138ce42304aSGreg Roach     *
139ce42304aSGreg Roach     * @return string
140ce42304aSGreg Roach     */
141ce42304aSGreg Roach    private function updateGedcom(GedcomRecord $record): string
142ce42304aSGreg Roach    {
143ce42304aSGreg Roach        return $record->gedcom() . "\n1 DEAT Y";
144ce42304aSGreg Roach    }
145ce42304aSGreg Roach}
146