. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function assert; /** * Process a form to create a new submitter. */ class CreateSubmitterAction implements RequestHandlerInterface { /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree); $params = (array) $request->getParsedBody(); $name = $params['submitter_name']; $address = $params['submitter_address']; $email = $params['submitter_email']; $phone = $params['submitter_phone']; $restriction = $params['restriction']; // Fix non-printing characters $name = trim(preg_replace('/\s+/', ' ', $name)); $gedcom = "0 @@ SUBM\n1 NAME " . $name; if ($address !== '') { $gedcom .= "\n1 ADDR " . $address; } if ($email !== '') { $gedcom .= "\n1 EMAIL " . $email; } if ($phone !== '') { $gedcom .= "\n1 PHON " . $phone; } if (in_array($restriction, ['none', 'privacy', 'confidential', 'locked'], true)) { $gedcom .= "\n1 RESN " . $restriction; } $record = $tree->createRecord($gedcom); $record = Registry::submitterFactory()->new($record->xref(), $record->gedcom(), null, $tree); // id and text are for select2 / autocomplete // html is for interactive modals return response([ 'id' => '@' . $record->xref() . '@', 'text' => view('selects/submitter', [ 'submitter' => $record, ]), 'html' => view('modals/record-created', [ 'title' => I18N::translate('The submitter has been created'), 'name' => $record->fullName(), 'url' => $record->url(), ]), ]); } }