xref: /webtrees/app/Http/RequestHandlers/DeleteRecord.php (revision 1581e36eec57f11b12a03a1213501f41df10ee12)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\GedcomRecord;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Tree;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function assert;
33use function is_string;
34use function preg_match;
35use function preg_match_all;
36use function preg_replace;
37use function response;
38
39/**
40 * Controller for edit forms and responses.
41 */
42class DeleteRecord implements RequestHandlerInterface
43{
44    /**
45     * Delete a record.
46     *
47     * @param ServerRequestInterface $request
48     *
49     * @return ResponseInterface
50     */
51    public function handle(ServerRequestInterface $request): ResponseInterface
52    {
53        $tree = $request->getAttribute('tree');
54        assert($tree instanceof Tree);
55
56        $xref = $request->getAttribute('xref');
57        assert(is_string($xref));
58
59        $record = GedcomRecord::getInstance($xref, $tree);
60        $record = Auth::checkRecordAccess($record, true);
61
62        if ($record && Auth::isEditor($record->tree()) && $record->canShow() && $record->canEdit()) {
63            // Delete links to this record
64            foreach ($record->linkingRecords() as $linker) {
65                $old_gedcom = $linker->gedcom();
66                $new_gedcom = $this->removeLinks($old_gedcom, $record->xref());
67                if ($old_gedcom !== $new_gedcom) {
68                    // If we have removed a link from a family to an individual, and it has only one member
69                    if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ FAM/', $new_gedcom, $fmatch) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1) {
70                        // Delete the family
71                        $family = GedcomRecord::getInstance($fmatch[1], $tree);
72                        /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */
73                        FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $family->fullName()));
74                        $family->deleteRecord();
75                        // Delete any remaining link to this family
76                        if ($match) {
77                            $relict     = GedcomRecord::getInstance($match[2][0], $tree);
78                            $new_gedcom = $relict->gedcom();
79                            $new_gedcom = $this->removeLinks($new_gedcom, $linker->xref());
80                            $relict->updateRecord($new_gedcom, false);
81                            /* I18N: %s are names of records, such as sources, repositories or individuals */
82                            FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', $relict->fullName(), $family->fullName()));
83                        }
84                    } else {
85                        // Remove links from $linker to $record
86                        /* I18N: %s are names of records, such as sources, repositories or individuals */
87                        FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', $linker->fullName(), $record->fullName()));
88                        $linker->updateRecord($new_gedcom, false);
89                    }
90                }
91            }
92            // Delete the record itself
93            $record->deleteRecord();
94        }
95
96        return response();
97    }
98
99    /**
100     * Remove all links from $gedrec to $xref, and any sub-tags.
101     *
102     * @param string $gedrec
103     * @param string $xref
104     *
105     * @return string
106     */
107    private function removeLinks($gedrec, $xref): string
108    {
109        $gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec);
110        $gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec);
111        $gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec);
112        $gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec);
113        $gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec);
114
115        return $gedrec;
116    }
117}
118