xref: /webtrees/app/Http/RequestHandlers/CreateRepositoryAction.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1852ede8cSGreg Roach<?php
2852ede8cSGreg Roach
3852ede8cSGreg Roach/**
4852ede8cSGreg Roach * webtrees: online genealogy
5*5bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6852ede8cSGreg Roach * This program is free software: you can redistribute it and/or modify
7852ede8cSGreg Roach * it under the terms of the GNU General Public License as published by
8852ede8cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9852ede8cSGreg Roach * (at your option) any later version.
10852ede8cSGreg Roach * This program is distributed in the hope that it will be useful,
11852ede8cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12852ede8cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13852ede8cSGreg Roach * GNU General Public License for more details.
14852ede8cSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16852ede8cSGreg Roach */
17852ede8cSGreg Roach
18852ede8cSGreg Roachdeclare(strict_types=1);
19852ede8cSGreg Roach
20852ede8cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21852ede8cSGreg Roach
22852ede8cSGreg Roachuse Fisharebest\Webtrees\I18N;
236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
24b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
25852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface;
26852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28852ede8cSGreg Roach
29e35de89cSGreg Roachuse function in_array;
30e35de89cSGreg Roachuse function preg_replace;
31e35de89cSGreg Roachuse function response;
32e35de89cSGreg Roachuse function trim;
33e35de89cSGreg Roachuse function view;
34852ede8cSGreg Roach
35852ede8cSGreg Roach/**
36852ede8cSGreg Roach * Process a form to create a new repository.
37852ede8cSGreg Roach */
38852ede8cSGreg Roachclass CreateRepositoryAction implements RequestHandlerInterface
39852ede8cSGreg Roach{
40852ede8cSGreg Roach    /**
41852ede8cSGreg Roach     * @param ServerRequestInterface $request
42852ede8cSGreg Roach     *
43852ede8cSGreg Roach     * @return ResponseInterface
44852ede8cSGreg Roach     */
45852ede8cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
46852ede8cSGreg Roach    {
47b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
48b46c87bdSGreg Roach        $params      = (array) $request->getParsedBody();
492d111ef4SGreg Roach        $name        = $params['name'];
502d111ef4SGreg Roach        $address     = $params['address'];
512d111ef4SGreg Roach        $url         = $params['url'];
52fc34a24dSGreg Roach        $restriction = $params['restriction'];
53852ede8cSGreg Roach
542b44f6fbSGreg Roach        // Fix non-printing characters
55852ede8cSGreg Roach        $name = trim(preg_replace('/\s+/', ' ', $name));
56852ede8cSGreg Roach
57852ede8cSGreg Roach        $gedcom = "0 @@ REPO\n1 NAME " . $name;
58852ede8cSGreg Roach
592d111ef4SGreg Roach        if ($address !== '') {
602d111ef4SGreg Roach            $gedcom .= "\n1 ADDR " . strtr($address, ["\r\n" => "\n2 CONT "]);
612d111ef4SGreg Roach        }
622d111ef4SGreg Roach
632d111ef4SGreg Roach        if ($url !== '') {
642d111ef4SGreg Roach            $gedcom .= "\n1 WWW " . $url;
652d111ef4SGreg Roach        }
662d111ef4SGreg Roach
67fc34a24dSGreg Roach        if (in_array($restriction, ['none', 'privacy', 'confidential', 'locked'], true)) {
68fc34a24dSGreg Roach            $gedcom .= "\n1 RESN " . $restriction;
69852ede8cSGreg Roach        }
70852ede8cSGreg Roach
71852ede8cSGreg Roach        $record = $tree->createRecord($gedcom);
726b9cb339SGreg Roach        $record = Registry::repositoryFactory()->new($record->xref(), $record->gedcom(), null, $tree);
73852ede8cSGreg Roach
74c8d78f19SGreg Roach        // value and text are for autocomplete
75852ede8cSGreg Roach        // html is for interactive modals
76852ede8cSGreg Roach        return response([
77c8d78f19SGreg Roach            'value' => '@' . $record->xref() . '@',
78c8d78f19SGreg Roach            'text'  => view('selects/repository', ['repository' => $record]),
79852ede8cSGreg Roach            'html'  => view('modals/record-created', [
80852ede8cSGreg Roach                'title' => I18N::translate('The repository has been created'),
81852ede8cSGreg Roach                'name'  => $record->fullName(),
82852ede8cSGreg Roach                'url'   => $record->url(),
83852ede8cSGreg Roach            ]),
84852ede8cSGreg Roach        ]);
85852ede8cSGreg Roach    }
86852ede8cSGreg Roach}
87