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\Tree; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function assert; 35use function e; 36use function is_string; 37use function preg_match_all; 38use function preg_replace; 39use function response; 40use function sprintf; 41 42/** 43 * Controller for edit forms and responses. 44 */ 45class DeleteRecord implements RequestHandlerInterface 46{ 47 /** 48 * Delete a record. 49 * 50 * @param ServerRequestInterface $request 51 * 52 * @return ResponseInterface 53 */ 54 public function handle(ServerRequestInterface $request): ResponseInterface 55 { 56 $tree = $request->getAttribute('tree'); 57 assert($tree instanceof Tree); 58 59 $xref = $request->getAttribute('xref'); 60 assert(is_string($xref)); 61 62 $record = Registry::gedcomRecordFactory()->make($xref, $tree); 63 $record = Auth::checkRecordAccess($record, true); 64 65 if (Auth::isEditor($record->tree()) && $record->canShow() && $record->canEdit()) { 66 // Delete links to this record 67 foreach ($record->linkingRecords() as $linker) { 68 $old_gedcom = $linker->gedcom(); 69 $new_gedcom = $this->removeLinks($old_gedcom, $record->xref()); 70 if ($old_gedcom !== $new_gedcom) { 71 // If we have removed a link from a family to an individual, and it now has only one member 72 if ($linker instanceof Family && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1) { 73 // Delete the family 74 /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */ 75 FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $linker->fullName())); 76 $linker->deleteRecord(); 77 // Delete the remaining link to this family 78 $relict = Registry::gedcomRecordFactory()->make($match[2][0], $tree); 79 if ($relict instanceof Individual) { 80 $relict_gedcom = $this->removeLinks($relict->gedcom(), $linker->xref()); 81 $relict->updateRecord($relict_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()), $linker->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(string $gedrec, string $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