. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Factory; use Fisharebest\Webtrees\FlashMessages; use Fisharebest\Webtrees\Gedcom; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function assert; use function is_string; use function preg_match; use function preg_match_all; use function preg_replace; use function response; use function sprintf; /** * Controller for edit forms and responses. */ class DeleteRecord implements RequestHandlerInterface { /** * Delete a record. * * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree); $xref = $request->getAttribute('xref'); assert(is_string($xref)); $record = Factory::gedcomRecord()->make($xref, $tree); $record = Auth::checkRecordAccess($record, true); if ($record && Auth::isEditor($record->tree()) && $record->canShow() && $record->canEdit()) { // Delete links to this record foreach ($record->linkingRecords() as $linker) { $old_gedcom = $linker->gedcom(); $new_gedcom = $this->removeLinks($old_gedcom, $record->xref()); if ($old_gedcom !== $new_gedcom) { // If we have removed a link from a family to an individual, and it has only one member if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ FAM/', $new_gedcom, $fmatch) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) === 1) { // Delete the family $family = Factory::gedcomRecord()->make($fmatch[1], $tree); /* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */ FlashMessages::addMessage(I18N::translate('The family “%s” has been deleted because it only has one member.', $family->fullName())); $family->deleteRecord(); // Delete any remaining link to this family if ($match) { $relict = Factory::gedcomRecord()->make($match[2][0], $tree); $new_gedcom = $relict->gedcom(); $new_gedcom = $this->removeLinks($new_gedcom, $linker->xref()); $relict->updateRecord($new_gedcom, false); /* I18N: %s are names of records, such as sources, repositories or individuals */ FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', sprintf('%2$s', e($relict->url()), $relict->fullName()), $family->fullName())); } } else { // Remove links from $linker to $record /* I18N: %s are names of records, such as sources, repositories or individuals */ FlashMessages::addMessage(I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', sprintf('%2$s', e($linker->url()), $linker->fullName()), $record->fullName())); $linker->updateRecord($new_gedcom, false); } } } // Delete the record itself $record->deleteRecord(); } return response(); } /** * Remove all links from $gedrec to $xref, and any sub-tags. * * @param string $gedrec * @param string $xref * * @return string */ private function removeLinks($gedrec, $xref): string { $gedrec = preg_replace('/\n1 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[2-9].*)*/', '', $gedrec); $gedrec = preg_replace('/\n2 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[3-9].*)*/', '', $gedrec); $gedrec = preg_replace('/\n3 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[4-9].*)*/', '', $gedrec); $gedrec = preg_replace('/\n4 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[5-9].*)*/', '', $gedrec); $gedrec = preg_replace('/\n5 ' . Gedcom::REGEX_TAG . ' @' . $xref . '@(\n[6-9].*)*/', '', $gedrec); return $gedrec; } }