xref: /webtrees/app/Http/RequestHandlers/DeleteRecord.php (revision 3b3db8adf3fd54bb8a196a781da9123905e54adf)
1*3b3db8adSGreg Roach<?php
2*3b3db8adSGreg Roach
3*3b3db8adSGreg Roach/**
4*3b3db8adSGreg Roach * webtrees: online genealogy
5*3b3db8adSGreg Roach * Copyright (C) 2019 webtrees development team
6*3b3db8adSGreg Roach * This program is free software: you can redistribute it and/or modify
7*3b3db8adSGreg Roach * it under the terms of the GNU General Public License as published by
8*3b3db8adSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*3b3db8adSGreg Roach * (at your option) any later version.
10*3b3db8adSGreg Roach * This program is distributed in the hope that it will be useful,
11*3b3db8adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*3b3db8adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*3b3db8adSGreg Roach * GNU General Public License for more details.
14*3b3db8adSGreg Roach * You should have received a copy of the GNU General Public License
15*3b3db8adSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*3b3db8adSGreg Roach */
17*3b3db8adSGreg Roach
18*3b3db8adSGreg Roachdeclare(strict_types=1);
19*3b3db8adSGreg Roach
20*3b3db8adSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*3b3db8adSGreg Roach
22*3b3db8adSGreg Roachuse Fisharebest\Webtrees\Auth;
23*3b3db8adSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24*3b3db8adSGreg Roachuse Fisharebest\Webtrees\Gedcom;
25*3b3db8adSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
26*3b3db8adSGreg Roachuse Fisharebest\Webtrees\I18N;
27*3b3db8adSGreg Roachuse Fisharebest\Webtrees\Tree;
28*3b3db8adSGreg Roachuse Psr\Http\Message\ResponseInterface;
29*3b3db8adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30*3b3db8adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31*3b3db8adSGreg Roach
32*3b3db8adSGreg Roachuse function assert;
33*3b3db8adSGreg Roachuse function preg_match;
34*3b3db8adSGreg Roachuse function preg_match_all;
35*3b3db8adSGreg Roachuse function preg_replace;
36*3b3db8adSGreg Roachuse function response;
37*3b3db8adSGreg Roach
38*3b3db8adSGreg Roach/**
39*3b3db8adSGreg Roach * Controller for edit forms and responses.
40*3b3db8adSGreg Roach */
41*3b3db8adSGreg Roachclass DeleteRecord implements RequestHandlerInterface
42*3b3db8adSGreg Roach{
43*3b3db8adSGreg Roach    /**
44*3b3db8adSGreg Roach     * Delete a record.
45*3b3db8adSGreg Roach     *
46*3b3db8adSGreg Roach     * @param ServerRequestInterface $request
47*3b3db8adSGreg Roach     *
48*3b3db8adSGreg Roach     * @return ResponseInterface
49*3b3db8adSGreg Roach     */
50*3b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
51*3b3db8adSGreg Roach    {
52*3b3db8adSGreg Roach        $tree = $request->getAttribute('tree');
53*3b3db8adSGreg Roach        assert($tree instanceof Tree);
54*3b3db8adSGreg Roach
55*3b3db8adSGreg Roach        $xref   = $request->getAttribute('xref');
56*3b3db8adSGreg Roach        $record = GedcomRecord::getInstance($xref, $tree);
57*3b3db8adSGreg Roach
58*3b3db8adSGreg Roach        Auth::checkRecordAccess($record, true);
59*3b3db8adSGreg Roach
60*3b3db8adSGreg Roach        if ($record && Auth::isEditor($record->tree()) && $record->canShow() && $record->canEdit()) {
61*3b3db8adSGreg Roach            // Delete links to this record
62*3b3db8adSGreg Roach            foreach ($record->linkingRecords() as $linker) {
63*3b3db8adSGreg Roach                $old_gedcom = $linker->gedcom();
64*3b3db8adSGreg Roach                $new_gedcom = $this->removeLinks($old_gedcom, $record->xref());
65*3b3db8adSGreg Roach                if ($old_gedcom !== $new_gedcom) {
66*3b3db8adSGreg Roach                    // If we have removed a link from a family to an individual, and it has only one member
67*3b3db8adSGreg Roach                    if (preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ FAM/', $new_gedcom) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) == 1) {
68*3b3db8adSGreg Roach                        // Delete the family
69*3b3db8adSGreg Roach                        $family = GedcomRecord::getInstance($xref, $tree);
70*3b3db8adSGreg Roach                        /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */
71*3b3db8adSGreg Roach                        FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $family->fullName()));
72*3b3db8adSGreg Roach                        $family->deleteRecord();
73*3b3db8adSGreg Roach                        // Delete any remaining link to this family
74*3b3db8adSGreg Roach                        if ($match) {
75*3b3db8adSGreg Roach                            $relict     = GedcomRecord::getInstance($match[2][0], $tree);
76*3b3db8adSGreg Roach                            $new_gedcom = $relict->gedcom();
77*3b3db8adSGreg Roach                            $new_gedcom = $this->removeLinks($new_gedcom, $linker->xref());
78*3b3db8adSGreg Roach                            $relict->updateRecord($new_gedcom, false);
79*3b3db8adSGreg Roach                            /* I18N: %s are names of records, such as sources, repositories or individuals */
80*3b3db8adSGreg Roach                            FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', $relict->fullName(), $family->fullName()));
81*3b3db8adSGreg Roach                        }
82*3b3db8adSGreg Roach                    } else {
83*3b3db8adSGreg Roach                        // Remove links from $linker to $record
84*3b3db8adSGreg Roach                        /* I18N: %s are names of records, such as sources, repositories or individuals */
85*3b3db8adSGreg Roach                        FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', $linker->fullName(), $record->fullName()));
86*3b3db8adSGreg Roach                        $linker->updateRecord($new_gedcom, false);
87*3b3db8adSGreg Roach                    }
88*3b3db8adSGreg Roach                }
89*3b3db8adSGreg Roach            }
90*3b3db8adSGreg Roach            // Delete the record itself
91*3b3db8adSGreg Roach            $record->deleteRecord();
92*3b3db8adSGreg Roach        }
93*3b3db8adSGreg Roach
94*3b3db8adSGreg Roach        return response();
95*3b3db8adSGreg Roach    }
96*3b3db8adSGreg Roach
97*3b3db8adSGreg Roach    /**
98*3b3db8adSGreg Roach     * Remove all links from $gedrec to $xref, and any sub-tags.
99*3b3db8adSGreg Roach     *
100*3b3db8adSGreg Roach     * @param string $gedrec
101*3b3db8adSGreg Roach     * @param string $xref
102*3b3db8adSGreg Roach     *
103*3b3db8adSGreg Roach     * @return string
104*3b3db8adSGreg Roach     */
105*3b3db8adSGreg Roach    private function removeLinks($gedrec, $xref): string
106*3b3db8adSGreg Roach    {
107*3b3db8adSGreg Roach        $gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec);
108*3b3db8adSGreg Roach        $gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec);
109*3b3db8adSGreg Roach        $gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec);
110*3b3db8adSGreg Roach        $gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec);
111*3b3db8adSGreg Roach        $gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec);
112*3b3db8adSGreg Roach
113*3b3db8adSGreg Roach        return $gedrec;
114*3b3db8adSGreg Roach    }
115*3b3db8adSGreg Roach}
116