-
Notifications
You must be signed in to change notification settings - Fork 302
/
DeleteRecord.php
127 lines (115 loc) · 5.65 KB
/
DeleteRecord.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2023 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\RequestHandlers;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\LinkedRecordService;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function e;
use function preg_match_all;
use function preg_replace;
use function response;
use function sprintf;
/**
* Delete a record.
*/
class DeleteRecord implements RequestHandlerInterface
{
private LinkedRecordService $linked_record_service;
/**
* @param LinkedRecordService $linked_record_service
*/
public function __construct(LinkedRecordService $linked_record_service)
{
$this->linked_record_service = $linked_record_service;
}
/**
* Delete a record.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$tree = Validator::attributes($request)->tree();
$xref = Validator::attributes($request)->isXref()->string('xref');
$record = Registry::gedcomRecordFactory()->make($xref, $tree);
$record = Auth::checkRecordAccess($record, true);
if (Auth::isEditor($record->tree()) && $record->canShow() && $record->canEdit()) {
// Delete links to this record
foreach ($this->linked_record_service->allLinkedRecords($record) as $linker) {
$old_gedcom = $linker->gedcom();
$new_gedcom = $this->removeLinks($old_gedcom, $record->xref());
if ($old_gedcom !== $new_gedcom) {
// If we have removed a link from a family to an individual, and it now has only one member and no genealogy facts
if (
$linker instanceof Family &&
preg_match('/\n1 (ANUL|CENS|DIV|DIVF|ENGA|MAR[BCLRS]|RESI|EVEN)/', $new_gedcom, $match) !== 1 &&
preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1
) {
// Delete the family
/* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */
FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $linker->fullName()));
$linker->deleteRecord();
// Delete the remaining link to this family
$relict = Registry::gedcomRecordFactory()->make($match[2][0], $tree);
if ($relict instanceof Individual) {
$relict_gedcom = $this->removeLinks($relict->gedcom(), $linker->xref());
$relict->updateRecord($relict_gedcom, false);
/* I18N: %s are names of records, such as sources, repositories or individuals */
FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', sprintf('<a href="%1$s" class="alert-link">%2$s</a>', e($relict->url()), $relict->fullName()), $linker->fullName()));
}
} else {
// Remove links from $linker to $record
/* I18N: %s are names of records, such as sources, repositories or individuals */
FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', sprintf('<a href="%1$s" class="alert-link">%2$s</a>', e($linker->url()), $linker->fullName()), $record->fullName()));
$linker->updateRecord($new_gedcom, false);
}
}
}
// Delete the record itself
$record->deleteRecord();
}
return response();
}
/**
* Remove all links from $gedrec to $xref, and any sub-tags.
*
* @param string $gedrec
* @param string $xref
*
* @return string
*/
private function removeLinks(string $gedrec, string $xref): string
{
$gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec);
$gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec);
return $gedrec;
}
}