diff options
| author | Greg Roach <fisharebest@webtrees.net> | 2019-10-28 20:17:46 +0000 |
|---|---|---|
| committer | Greg Roach <fisharebest@webtrees.net> | 2019-10-28 21:11:32 +0000 |
| commit | 3b3db8adf3fd54bb8a196a781da9123905e54adf (patch) | |
| tree | 5bde4709c771ea00529f79f8f45652c49a3a7c10 /app/Http/RequestHandlers/DeleteRecord.php | |
| parent | fc1d5378722fa0e1e930dd6d4c5742e419121965 (diff) | |
| download | webtrees-3b3db8adf3fd54bb8a196a781da9123905e54adf.tar.gz webtrees-3b3db8adf3fd54bb8a196a781da9123905e54adf.tar.bz2 webtrees-3b3db8adf3fd54bb8a196a781da9123905e54adf.zip | |
Fix: #2705 - Break EditGedcomRecordController into request handlers
Diffstat (limited to 'app/Http/RequestHandlers/DeleteRecord.php')
| -rw-r--r-- | app/Http/RequestHandlers/DeleteRecord.php | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/app/Http/RequestHandlers/DeleteRecord.php b/app/Http/RequestHandlers/DeleteRecord.php new file mode 100644 index 0000000000..82c4d75c93 --- /dev/null +++ b/app/Http/RequestHandlers/DeleteRecord.php @@ -0,0 +1,115 @@ +<?php + +/** + * webtrees: online genealogy + * Copyright (C) 2019 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +declare(strict_types=1); + +namespace Fisharebest\Webtrees\Http\RequestHandlers; + +use Fisharebest\Webtrees\Auth; +use Fisharebest\Webtrees\FlashMessages; +use Fisharebest\Webtrees\Gedcom; +use Fisharebest\Webtrees\GedcomRecord; +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 preg_match; +use function preg_match_all; +use function preg_replace; +use function response; + +/** + * 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'); + $record = GedcomRecord::getInstance($xref, $tree); + + 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) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $new_gedcom, $match) == 1) { + // Delete the family + $family = GedcomRecord::getInstance($xref, $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 = GedcomRecord::getInstance($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.', $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.', $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; + } +} |
