xref: /webtrees/app/Http/RequestHandlers/DeleteRecord.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
13b3db8adSGreg Roach<?php
23b3db8adSGreg Roach
33b3db8adSGreg Roach/**
43b3db8adSGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
63b3db8adSGreg Roach * This program is free software: you can redistribute it and/or modify
73b3db8adSGreg Roach * it under the terms of the GNU General Public License as published by
83b3db8adSGreg Roach * the Free Software Foundation, either version 3 of the License, or
93b3db8adSGreg Roach * (at your option) any later version.
103b3db8adSGreg Roach * This program is distributed in the hope that it will be useful,
113b3db8adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123b3db8adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133b3db8adSGreg Roach * GNU General Public License for more details.
143b3db8adSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
163b3db8adSGreg Roach */
173b3db8adSGreg Roach
183b3db8adSGreg Roachdeclare(strict_types=1);
193b3db8adSGreg Roach
203b3db8adSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
213b3db8adSGreg Roach
223b3db8adSGreg Roachuse Fisharebest\Webtrees\Auth;
238553b2e2SGreg Roachuse Fisharebest\Webtrees\Family;
243b3db8adSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
253b3db8adSGreg Roachuse Fisharebest\Webtrees\Gedcom;
263b3db8adSGreg Roachuse Fisharebest\Webtrees\I18N;
278553b2e2SGreg Roachuse Fisharebest\Webtrees\Individual;
286b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
294991f205SGreg Roachuse Fisharebest\Webtrees\Services\LinkedRecordService;
30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
313b3db8adSGreg Roachuse Psr\Http\Message\ResponseInterface;
323b3db8adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
333b3db8adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
343b3db8adSGreg Roach
358553b2e2SGreg Roachuse function e;
363b3db8adSGreg Roachuse function preg_match_all;
373b3db8adSGreg Roachuse function preg_replace;
383b3db8adSGreg Roachuse function response;
39db49063cSThomas Karcheruse function sprintf;
403b3db8adSGreg Roach
413b3db8adSGreg Roach/**
424991f205SGreg Roach * Delete a record.
433b3db8adSGreg Roach */
443b3db8adSGreg Roachclass DeleteRecord implements RequestHandlerInterface
453b3db8adSGreg Roach{
464991f205SGreg Roach    private LinkedRecordService $linked_record_service;
474991f205SGreg Roach
484991f205SGreg Roach    /**
494991f205SGreg Roach     * @param LinkedRecordService $linked_record_service
504991f205SGreg Roach     */
514991f205SGreg Roach    public function __construct(LinkedRecordService $linked_record_service)
524991f205SGreg Roach    {
534991f205SGreg Roach        $this->linked_record_service = $linked_record_service;
544991f205SGreg Roach    }
554991f205SGreg Roach
563b3db8adSGreg Roach    /**
573b3db8adSGreg Roach     * Delete a record.
583b3db8adSGreg Roach     *
593b3db8adSGreg Roach     * @param ServerRequestInterface $request
603b3db8adSGreg Roach     *
613b3db8adSGreg Roach     * @return ResponseInterface
623b3db8adSGreg Roach     */
633b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
643b3db8adSGreg Roach    {
65b55cbc6bSGreg Roach        $tree   = Validator::attributes($request)->tree();
66b55cbc6bSGreg Roach        $xref   = Validator::attributes($request)->isXref()->string('xref');
676b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
68ddeb3354SGreg Roach        $record = Auth::checkRecordAccess($record, true);
693b3db8adSGreg Roach
70ac701fbdSGreg Roach        if (Auth::isEditor($record->tree()) && $record->canShow() && $record->canEdit()) {
713b3db8adSGreg Roach            // Delete links to this record
724991f205SGreg Roach            foreach ($this->linked_record_service->allLinkedRecords($record) as $linker) {
733b3db8adSGreg Roach                $old_gedcom = $linker->gedcom();
743b3db8adSGreg Roach                $new_gedcom = $this->removeLinks($old_gedcom, $record->xref());
753b3db8adSGreg Roach                if ($old_gedcom !== $new_gedcom) {
768553b2e2SGreg Roach                    // If we have removed a link from a family to an individual, and it now has only one member
778553b2e2SGreg Roach                    if ($linker instanceof Family && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1) {
783b3db8adSGreg Roach                        // Delete the family
793b3db8adSGreg Roach                        /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */
808553b2e2SGreg Roach                        FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $linker->fullName()));
818553b2e2SGreg Roach                        $linker->deleteRecord();
828553b2e2SGreg Roach                        // Delete the remaining link to this family
836b9cb339SGreg Roach                        $relict = Registry::gedcomRecordFactory()->make($match[2][0], $tree);
848553b2e2SGreg Roach                        if ($relict instanceof Individual) {
858553b2e2SGreg Roach                            $relict_gedcom = $this->removeLinks($relict->gedcom(), $linker->xref());
868553b2e2SGreg Roach                            $relict->updateRecord($relict_gedcom, false);
873b3db8adSGreg Roach                            /* I18N: %s are names of records, such as sources, repositories or individuals */
888553b2e2SGreg Roach                            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()));
893b3db8adSGreg Roach                        }
903b3db8adSGreg Roach                    } else {
913b3db8adSGreg Roach                        // Remove links from $linker to $record
923b3db8adSGreg Roach                        /* I18N: %s are names of records, such as sources, repositories or individuals */
93db49063cSThomas Karcher                        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()));
943b3db8adSGreg Roach                        $linker->updateRecord($new_gedcom, false);
953b3db8adSGreg Roach                    }
963b3db8adSGreg Roach                }
973b3db8adSGreg Roach            }
983b3db8adSGreg Roach            // Delete the record itself
993b3db8adSGreg Roach            $record->deleteRecord();
1003b3db8adSGreg Roach        }
1013b3db8adSGreg Roach
1023b3db8adSGreg Roach        return response();
1033b3db8adSGreg Roach    }
1043b3db8adSGreg Roach
1053b3db8adSGreg Roach    /**
1063b3db8adSGreg Roach     * Remove all links from $gedrec to $xref, and any sub-tags.
1073b3db8adSGreg Roach     *
1083b3db8adSGreg Roach     * @param string $gedrec
1093b3db8adSGreg Roach     * @param string $xref
1103b3db8adSGreg Roach     *
1113b3db8adSGreg Roach     * @return string
1123b3db8adSGreg Roach     */
11324f2a3afSGreg Roach    private function removeLinks(string $gedrec, string $xref): string
1143b3db8adSGreg Roach    {
1153b3db8adSGreg Roach        $gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec);
1163b3db8adSGreg Roach        $gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec);
1173b3db8adSGreg Roach        $gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec);
1183b3db8adSGreg Roach        $gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec);
1193b3db8adSGreg Roach        $gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec);
1203b3db8adSGreg Roach
1213b3db8adSGreg Roach        return $gedrec;
1223b3db8adSGreg Roach    }
1233b3db8adSGreg Roach}
124