xref: /webtrees/app/Http/RequestHandlers/CreateRepositoryModal.php (revision d4265d07c02c7f5ca2855fe55f3ca0b73a86c7a9)
1852ede8cSGreg Roach<?php
2852ede8cSGreg Roach
3852ede8cSGreg Roach/**
4852ede8cSGreg Roach * webtrees: online genealogy
5852ede8cSGreg Roach * Copyright (C) 2019 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
15852ede8cSGreg Roach * along with this program. If not, see <http://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;
23852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree;
24852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface;
25852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
27852ede8cSGreg Roach
28852ede8cSGreg Roachuse function assert;
29*d4265d07SGreg Roachuse function response;
30*d4265d07SGreg Roachuse function view;
31852ede8cSGreg Roach
32852ede8cSGreg Roach/**
33852ede8cSGreg Roach * Show a form to create a new repository.
34852ede8cSGreg Roach */
35852ede8cSGreg Roachclass CreateRepositoryModal implements RequestHandlerInterface
36852ede8cSGreg Roach{
37852ede8cSGreg Roach    /**
38852ede8cSGreg Roach     * @param ServerRequestInterface $request
39852ede8cSGreg Roach     *
40852ede8cSGreg Roach     * @return ResponseInterface
41852ede8cSGreg Roach     */
42852ede8cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
43852ede8cSGreg Roach    {
44852ede8cSGreg Roach        $tree = $request->getAttribute('tree');
45852ede8cSGreg Roach        assert($tree instanceof Tree);
46852ede8cSGreg Roach
47852ede8cSGreg Roach        return response(view('modals/create-repository', [
48852ede8cSGreg Roach            'tree' => $tree,
49852ede8cSGreg Roach        ]));
50852ede8cSGreg Roach    }
51852ede8cSGreg Roach
52852ede8cSGreg Roach    /**
53852ede8cSGreg Roach     * Process a form to create a new repository.
54852ede8cSGreg Roach     *
55852ede8cSGreg Roach     * @param ServerRequestInterface $request
56852ede8cSGreg Roach     *
57852ede8cSGreg Roach     * @return ResponseInterface
58852ede8cSGreg Roach     */
59852ede8cSGreg Roach    public function createRepositoryAction(ServerRequestInterface $request): ResponseInterface
60852ede8cSGreg Roach    {
61852ede8cSGreg Roach        $tree = $request->getAttribute('tree');
62852ede8cSGreg Roach        assert($tree instanceof Tree);
63852ede8cSGreg Roach
64852ede8cSGreg Roach        $params              = $request->getParsedBody();
65852ede8cSGreg Roach        $name                = $params['repository-name'];
66852ede8cSGreg Roach        $privacy_restriction = $params['privacy-restriction'];
67852ede8cSGreg Roach        $edit_restriction    = $params['edit-restriction'];
68852ede8cSGreg Roach
69852ede8cSGreg Roach        // Fix whitespace
70852ede8cSGreg Roach        $name = trim(preg_replace('/\s+/', ' ', $name));
71852ede8cSGreg Roach
72852ede8cSGreg Roach        $gedcom = "0 @@ REPO\n1 NAME " . $name;
73852ede8cSGreg Roach
74852ede8cSGreg Roach        if (in_array($privacy_restriction, ['none', 'privacy', 'confidential'], true)) {
75852ede8cSGreg Roach            $gedcom .= "\n1 RESN " . $privacy_restriction;
76852ede8cSGreg Roach        }
77852ede8cSGreg Roach
78852ede8cSGreg Roach        if ($edit_restriction === 'locked') {
79852ede8cSGreg Roach            $gedcom .= "\n1 RESN " . $edit_restriction;
80852ede8cSGreg Roach        }
81852ede8cSGreg Roach
82852ede8cSGreg Roach        $record = $tree->createRecord($gedcom);
83852ede8cSGreg Roach
84852ede8cSGreg Roach        // id and text are for select2 / autocomplete
85852ede8cSGreg Roach        // html is for interactive modals
86852ede8cSGreg Roach        return response([
87852ede8cSGreg Roach            'id'   => $record->xref(),
88852ede8cSGreg Roach            'text' => view('selects/repository', [
89852ede8cSGreg Roach                'repository' => $record,
90852ede8cSGreg Roach            ]),
91852ede8cSGreg Roach            'html' => view('modals/record-created', [
92852ede8cSGreg Roach                'title' => I18N::translate('The repository has been created'),
93852ede8cSGreg Roach                'name'  => $record->fullName(),
94852ede8cSGreg Roach                'url'   => $record->url(),
95852ede8cSGreg Roach            ]),
96852ede8cSGreg Roach        ]);
97852ede8cSGreg Roach    }
98852ede8cSGreg Roach}
99