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 and no genealogy facts 77 if ( 78 $linker instanceof Family && 79 preg_match('/\n1 (ANUL|CENS|DIV|DIVF|ENGA|MAR[BCLRS]|RESI|EVEN)/', $new_gedcom, $match) !== 1 && 80 preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1 81 ) { 82 // Delete the family 83 /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */ 84 FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $linker->fullName())); 85 $linker->deleteRecord(); 86 // Delete the remaining link to this family 87 $relict = Registry::gedcomRecordFactory()->make($match[2][0], $tree); 88 if ($relict instanceof Individual) { 89 $relict_gedcom = $this->removeLinks($relict->gedcom(), $linker->xref()); 90 $relict->updateRecord($relict_gedcom, false); 91 /* I18N: %s are names of records, such as sources, repositories or individuals */ 92 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())); 93 } 94 } else { 95 // Remove links from $linker to $record 96 /* I18N: %s are names of records, such as sources, repositories or individuals */ 97 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())); 98 $linker->updateRecord($new_gedcom, false); 99 } 100 } 101 } 102 // Delete the record itself 103 $record->deleteRecord(); 104 } 105 106 return response(); 107 } 108 109 /** 110 * Remove all links from $gedrec to $xref, and any sub-tags. 111 * 112 * @param string $gedrec 113 * @param string $xref 114 * 115 * @return string 116 */ 117 private function removeLinks(string $gedrec, string $xref): string 118 { 119 $gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec); 120 $gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec); 121 $gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec); 122 $gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec); 123 $gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec); 124 125 return $gedrec; 126 } 127} 128