xref: /webtrees/app/Http/RequestHandlers/CreateSubmitterAction.php (revision 852ede8c85e501a9df6f3c9d31e5933428e18cc2)
1*852ede8cSGreg Roach<?php
2*852ede8cSGreg Roach
3*852ede8cSGreg Roach/**
4*852ede8cSGreg Roach * webtrees: online genealogy
5*852ede8cSGreg Roach * Copyright (C) 2019 webtrees development team
6*852ede8cSGreg Roach * This program is free software: you can redistribute it and/or modify
7*852ede8cSGreg Roach * it under the terms of the GNU General Public License as published by
8*852ede8cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*852ede8cSGreg Roach * (at your option) any later version.
10*852ede8cSGreg Roach * This program is distributed in the hope that it will be useful,
11*852ede8cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*852ede8cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*852ede8cSGreg Roach * GNU General Public License for more details.
14*852ede8cSGreg Roach * You should have received a copy of the GNU General Public License
15*852ede8cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*852ede8cSGreg Roach */
17*852ede8cSGreg Roach
18*852ede8cSGreg Roachdeclare(strict_types=1);
19*852ede8cSGreg Roach
20*852ede8cSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*852ede8cSGreg Roach
22*852ede8cSGreg Roachuse Fisharebest\Webtrees\I18N;
23*852ede8cSGreg Roachuse Fisharebest\Webtrees\Tree;
24*852ede8cSGreg Roachuse Psr\Http\Message\ResponseInterface;
25*852ede8cSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
26*852ede8cSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
27*852ede8cSGreg Roach
28*852ede8cSGreg Roachuse function assert;
29*852ede8cSGreg Roach
30*852ede8cSGreg Roach/**
31*852ede8cSGreg Roach * Process a form to create a new submitter.
32*852ede8cSGreg Roach */
33*852ede8cSGreg Roachclass CreateSubmitterAction implements RequestHandlerInterface
34*852ede8cSGreg Roach{
35*852ede8cSGreg Roach    /**
36*852ede8cSGreg Roach     * @param ServerRequestInterface $request
37*852ede8cSGreg Roach     *
38*852ede8cSGreg Roach     * @return ResponseInterface
39*852ede8cSGreg Roach     */
40*852ede8cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
41*852ede8cSGreg Roach    {
42*852ede8cSGreg Roach        $tree = $request->getAttribute('tree');
43*852ede8cSGreg Roach        assert($tree instanceof Tree);
44*852ede8cSGreg Roach
45*852ede8cSGreg Roach        $params              = $request->getParsedBody();
46*852ede8cSGreg Roach        $name                = $params['submitter_name'];
47*852ede8cSGreg Roach        $address             = $params['submitter_address'];
48*852ede8cSGreg Roach        $privacy_restriction = $params['privacy-restriction'];
49*852ede8cSGreg Roach        $edit_restriction    = $params['edit-restriction'];
50*852ede8cSGreg Roach
51*852ede8cSGreg Roach        // Fix whitespace
52*852ede8cSGreg Roach        $name = trim(preg_replace('/\s+/', ' ', $name));
53*852ede8cSGreg Roach
54*852ede8cSGreg Roach        // Convert line endings to GEDDCOM continuations
55*852ede8cSGreg Roach        $address = str_replace([
56*852ede8cSGreg Roach            "\r\n",
57*852ede8cSGreg Roach            "\r",
58*852ede8cSGreg Roach            "\n",
59*852ede8cSGreg Roach        ], "\n1 CONT ", $address);
60*852ede8cSGreg Roach
61*852ede8cSGreg Roach        $gedcom = "0 @@ SUBM\n1 NAME " . $name;
62*852ede8cSGreg Roach
63*852ede8cSGreg Roach        if ($address !== '') {
64*852ede8cSGreg Roach            $gedcom .= "\n1 ADDR " . $address;
65*852ede8cSGreg Roach        }
66*852ede8cSGreg Roach
67*852ede8cSGreg Roach        if (in_array($privacy_restriction, ['none', 'privacy', 'confidential'], true)) {
68*852ede8cSGreg Roach            $gedcom .= "\n1 RESN " . $privacy_restriction;
69*852ede8cSGreg Roach        }
70*852ede8cSGreg Roach
71*852ede8cSGreg Roach        if ($edit_restriction === 'locked') {
72*852ede8cSGreg Roach            $gedcom .= "\n1 RESN " . $edit_restriction;
73*852ede8cSGreg Roach        }
74*852ede8cSGreg Roach
75*852ede8cSGreg Roach        $record = $tree->createRecord($gedcom);
76*852ede8cSGreg Roach
77*852ede8cSGreg Roach        return response([
78*852ede8cSGreg Roach            'id'   => $record->xref(),
79*852ede8cSGreg Roach            'text' => view('selects/submitter', [
80*852ede8cSGreg Roach                'submitter' => $record,
81*852ede8cSGreg Roach            ]),
82*852ede8cSGreg Roach            'html' => view('modals/record-created', [
83*852ede8cSGreg Roach                'title' => I18N::translate('The submitter has been created'),
84*852ede8cSGreg Roach                'name'  => $record->fullName(),
85*852ede8cSGreg Roach                'url'   => $record->url(),
86*852ede8cSGreg Roach            ]),
87*852ede8cSGreg Roach        ]);
88*852ede8cSGreg Roach    }
89*852ede8cSGreg Roach}
90