. */ 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; use function is_string; /** * 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'); assert(is_string($xref)); $record = GedcomRecord::getInstance($xref, $tree); $record = Auth::checkRecordAccess($record, true); $params = (array) $request->getParsedBody(); $facts = $params['fact'] ?? []; $fact_ids = $params['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()); } }