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