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