. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Validator; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; /** * Process a form to create a new submitter. */ class CreateSubmitterAction implements RequestHandlerInterface { /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = Validator::attributes($request)->tree(); $name = Validator::parsedBody($request)->string('submitter_name'); $address = Validator::parsedBody($request)->string('submitter_address'); $email = Validator::parsedBody($request)->string('submitter_email'); $phone = Validator::parsedBody($request)->string('submitter_phone'); $restriction = Validator::parsedBody($request)->isInArray(['', 'NONE', 'PRIVACY', 'CONFIDENTIAL', 'LOCKED'])->string('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 ($restriction !== '') { $gedcom .= "\n1 RESN " . $restriction; } $record = $tree->createRecord($gedcom); $record = Registry::submitterFactory()->new($record->xref(), $record->gedcom(), null, $tree); // value and text are for autocomplete // html is for interactive modals return response([ 'value' => '@' . $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(), ]), ]); } }