13b3db8adSGreg Roach<?php 23b3db8adSGreg Roach 33b3db8adSGreg Roach/** 43b3db8adSGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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) { 76588fd84bSGreg Roach // If we have removed a link from a family to an individual, and it now has only one member and no genealogy facts 77588fd84bSGreg Roach if ( 78588fd84bSGreg Roach $linker instanceof Family && 79588fd84bSGreg Roach preg_match('/\n1 (ANUL|CENS|DIV|DIVF|ENGA|MAR[BCLRS]|RESI|EVEN)/', $new_gedcom, $match) !== 1 && 80588fd84bSGreg Roach preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1 81588fd84bSGreg Roach ) { 823b3db8adSGreg Roach // Delete the family 833b3db8adSGreg Roach /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */ 848553b2e2SGreg Roach FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $linker->fullName())); 858553b2e2SGreg Roach $linker->deleteRecord(); 868553b2e2SGreg Roach // Delete the remaining link to this family 876b9cb339SGreg Roach $relict = Registry::gedcomRecordFactory()->make($match[2][0], $tree); 888553b2e2SGreg Roach if ($relict instanceof Individual) { 898553b2e2SGreg Roach $relict_gedcom = $this->removeLinks($relict->gedcom(), $linker->xref()); 908553b2e2SGreg Roach $relict->updateRecord($relict_gedcom, false); 913b3db8adSGreg Roach /* I18N: %s are names of records, such as sources, repositories or individuals */ 928553b2e2SGreg 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())); 933b3db8adSGreg Roach } 943b3db8adSGreg Roach } else { 953b3db8adSGreg Roach // Remove links from $linker to $record 963b3db8adSGreg Roach /* I18N: %s are names of records, such as sources, repositories or individuals */ 97db49063cSThomas 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())); 983b3db8adSGreg Roach $linker->updateRecord($new_gedcom, false); 993b3db8adSGreg Roach } 1003b3db8adSGreg Roach } 1013b3db8adSGreg Roach } 1023b3db8adSGreg Roach // Delete the record itself 1033b3db8adSGreg Roach $record->deleteRecord(); 1043b3db8adSGreg Roach } 1053b3db8adSGreg Roach 1063b3db8adSGreg Roach return response(); 1073b3db8adSGreg Roach } 1083b3db8adSGreg Roach 1093b3db8adSGreg Roach /** 1103b3db8adSGreg Roach * Remove all links from $gedrec to $xref, and any sub-tags. 1113b3db8adSGreg Roach * 1123b3db8adSGreg Roach * @param string $gedrec 1133b3db8adSGreg Roach * @param string $xref 1143b3db8adSGreg Roach * 1153b3db8adSGreg Roach * @return string 1163b3db8adSGreg Roach */ 11724f2a3afSGreg Roach private function removeLinks(string $gedrec, string $xref): string 1183b3db8adSGreg Roach { 1193b3db8adSGreg Roach $gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec); 1203b3db8adSGreg Roach $gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec); 1213b3db8adSGreg Roach $gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec); 1223b3db8adSGreg Roach $gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec); 1233b3db8adSGreg Roach $gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec); 1243b3db8adSGreg Roach 1253b3db8adSGreg Roach return $gedrec; 1263b3db8adSGreg Roach } 1273b3db8adSGreg Roach} 128