. */ 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; use function in_array; use function preg_replace; use function response; use function trim; use function view; /** * Process a form to create a new repository. */ class CreateRepositoryAction 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['name']; $address = $params['address']; $url = $params['url']; $restriction = $params['restriction']; // Fix non-printing characters $name = trim(preg_replace('/\s+/', ' ', $name)); $gedcom = "0 @@ REPO\n1 NAME " . $name; if ($address !== '') { $gedcom .= "\n1 ADDR " . strtr($address, ["\r\n" => "\n2 CONT "]); } if ($url !== '') { $gedcom .= "\n1 WWW " . $url; } if (in_array($restriction, ['none', 'privacy', 'confidential', 'locked'], true)) { $gedcom .= "\n1 RESN " . $restriction; } $record = $tree->createRecord($gedcom); $record = Registry::repositoryFactory()->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/repository', ['repository' => $record]), 'html' => view('modals/record-created', [ 'title' => I18N::translate('The repository has been created'), 'name' => $record->fullName(), 'url' => $record->url(), ]), ]); } }