. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function assert; /** * Edit the raw GEDCOM of a record. */ class EditRawRecordAction implements RequestHandlerInterface { /** * @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); $facts = $request->getParsedBody()['fact'] ?? []; $fact_ids = $request->getParsedBody()['fact_id'] ?? []; $gedcom = '0 @' . $record->xref() . '@ ' . $record::RECORD_TYPE; // Retain any private facts foreach ($record->facts([], false, Auth::PRIV_HIDE) as $fact) { if (!in_array($fact->id(), $fact_ids, true) && !$fact->isPendingDeletion()) { $gedcom .= "\n" . $fact->gedcom(); } } // Append the updated facts foreach ($facts as $fact) { $gedcom .= "\n" . $fact; } // Empty lines and MSDOS line endings. $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); $gedcom = trim($gedcom); $record->updateRecord($gedcom, false); return redirect($record->url()); } }