xref: /webtrees/app/Http/RequestHandlers/DeleteRecord.php (revision 541cf44d763a8aa5906f3fce58ca6b0ac3a25986)
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\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\FlashMessages;
25use Fisharebest\Webtrees\Gedcom;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Registry;
29use Fisharebest\Webtrees\Validator;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function e;
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   = Validator::attributes($request)->tree();
55        $xref   = Validator::attributes($request)->isXref()->string('xref');
56        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
57        $record = Auth::checkRecordAccess($record, true);
58
59        if (Auth::isEditor($record->tree()) && $record->canShow() && $record->canEdit()) {
60            // Delete links to this record
61            foreach ($record->linkingRecords() as $linker) {
62                $old_gedcom = $linker->gedcom();
63                $new_gedcom = $this->removeLinks($old_gedcom, $record->xref());
64                if ($old_gedcom !== $new_gedcom) {
65                    // If we have removed a link from a family to an individual, and it now has only one member
66                    if ($linker instanceof Family && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1) {
67                        // Delete the family
68                        /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */
69                        FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $linker->fullName()));
70                        $linker->deleteRecord();
71                        // Delete the remaining link to this family
72                        $relict = Registry::gedcomRecordFactory()->make($match[2][0], $tree);
73                        if ($relict instanceof Individual) {
74                            $relict_gedcom = $this->removeLinks($relict->gedcom(), $linker->xref());
75                            $relict->updateRecord($relict_gedcom, false);
76                            /* I18N: %s are names of records, such as sources, repositories or individuals */
77                            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()));
78                        }
79                    } else {
80                        // Remove links from $linker to $record
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.', sprintf('<a href="%1$s" class="alert-link">%2$s</a>', e($linker->url()), $linker->fullName()), $record->fullName()));
83                        $linker->updateRecord($new_gedcom, false);
84                    }
85                }
86            }
87            // Delete the record itself
88            $record->deleteRecord();
89        }
90
91        return response();
92    }
93
94    /**
95     * Remove all links from $gedrec to $xref, and any sub-tags.
96     *
97     * @param string $gedrec
98     * @param string $xref
99     *
100     * @return string
101     */
102    private function removeLinks(string $gedrec, string $xref): string
103    {
104        $gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec);
105        $gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec);
106        $gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec);
107        $gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec);
108        $gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec);
109
110        return $gedrec;
111    }
112}
113