xref: /webtrees/app/Http/RequestHandlers/CreateSourceAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
1852ede8cSGreg Roach<?php
2852ede8cSGreg Roach
3852ede8cSGreg Roach/**
4852ede8cSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
29852ede8cSGreg Roach/**
30852ede8cSGreg Roach * Process a form to create a new source.
31852ede8cSGreg Roach */
32852ede8cSGreg Roachclass CreateSourceAction implements RequestHandlerInterface
33852ede8cSGreg Roach{
34852ede8cSGreg Roach    /**
35852ede8cSGreg Roach     * @param ServerRequestInterface $request
36852ede8cSGreg Roach     *
37852ede8cSGreg Roach     * @return ResponseInterface
38852ede8cSGreg Roach     */
39852ede8cSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
40852ede8cSGreg Roach    {
41b55cbc6bSGreg Roach        $tree         = Validator::attributes($request)->tree();
4203f0aef8SGreg Roach        $title        = Validator::parsedBody($request)->isNotEmpty()->string('source-title');
43748dbe15SGreg Roach        $abbreviation = Validator::parsedBody($request)->string('source-abbreviation');
44748dbe15SGreg Roach        $author       = Validator::parsedBody($request)->string('source-author');
45748dbe15SGreg Roach        $publication  = Validator::parsedBody($request)->string('source-publication');
4603f0aef8SGreg Roach        $repository   = Validator::parsedBody($request)->isXref()->string('source-repository', '');
47748dbe15SGreg Roach        $call_number  = Validator::parsedBody($request)->string('source-call-number');
48748dbe15SGreg Roach        $text         = Validator::parsedBody($request)->string('source-text');
4903f0aef8SGreg Roach        $restriction  = Validator::parsedBody($request)->string('restriction');
50852ede8cSGreg Roach
5103f0aef8SGreg Roach        $title        = Registry::elementFactory()->make('SOUR:TITL')->canonical($title);
5203f0aef8SGreg Roach        $abbreviation = Registry::elementFactory()->make('SOUR:ABBR')->canonical($abbreviation);
5303f0aef8SGreg Roach        $author       = Registry::elementFactory()->make('SOUR:AUTH')->canonical($author);
5403f0aef8SGreg Roach        $publication  = Registry::elementFactory()->make('SOUR:PUBL')->canonical($publication);
5503f0aef8SGreg Roach        $repository   = Registry::elementFactory()->make('SOUR:REPO')->canonical($repository);
5603f0aef8SGreg Roach        $call_number  = Registry::elementFactory()->make('SOUR:REPO:CALN')->canonical($call_number);
5703f0aef8SGreg Roach        $text         = Registry::elementFactory()->make('SOUR:TEXT')->canonical($text);
5803f0aef8SGreg Roach        $restriction  = Registry::elementFactory()->make('SOUR:RESN')->canonical($restriction);
59852ede8cSGreg Roach
6003f0aef8SGreg Roach        $gedcom = "0 @@ SOUR\n1 TITL " . strtr($title, ["\n" => "\n2 CONT "]);
61852ede8cSGreg Roach
62852ede8cSGreg Roach        if ($abbreviation !== '') {
6303f0aef8SGreg Roach            $gedcom .= "\n1 ABBR " . strtr($abbreviation, ["\n" => "\n2 CONT "]);
64852ede8cSGreg Roach        }
65852ede8cSGreg Roach
66852ede8cSGreg Roach        if ($author !== '') {
6703f0aef8SGreg Roach            $gedcom .= "\n1 AUTH " . strtr($author, ["\n" => "\n2 CONT "]);
68852ede8cSGreg Roach        }
69852ede8cSGreg Roach
70852ede8cSGreg Roach        if ($publication !== '') {
7103f0aef8SGreg Roach            $gedcom .= "\n1 PUBL " . strtr($publication, ["\n" => "\n2 CONT "]);
72852ede8cSGreg Roach        }
73852ede8cSGreg Roach
74852ede8cSGreg Roach        if ($text !== '') {
7503f0aef8SGreg Roach            $gedcom .= "\n1 TEXT " . strtr($text, ["\n" => "\n2 CONT "]);
76852ede8cSGreg Roach        }
77852ede8cSGreg Roach
78852ede8cSGreg Roach        if ($repository !== '') {
79852ede8cSGreg Roach            $gedcom .= "\n1 REPO @" . $repository . '@';
80852ede8cSGreg Roach
81852ede8cSGreg Roach            if ($call_number !== '') {
8203f0aef8SGreg Roach                $gedcom .= "\n2 CALN " . strtr($call_number, ["\n" => "\n3 CONT "]);
83852ede8cSGreg Roach            }
84852ede8cSGreg Roach        }
85852ede8cSGreg Roach
86e3132a19SGreg Roach        if ($restriction !== '') {
8703f0aef8SGreg Roach            $gedcom .= "\n1 RESN " . strtr($restriction, ["\n" => "\n2 CONT "]);
88852ede8cSGreg Roach        }
89852ede8cSGreg Roach
90852ede8cSGreg Roach        $record = $tree->createRecord($gedcom);
91852ede8cSGreg Roach
92c8d78f19SGreg Roach        // value and text are for autocomplete
93852ede8cSGreg Roach        // html is for interactive modals
94852ede8cSGreg Roach        return response([
95c8d78f19SGreg Roach            'value' => '@' . $record->xref() . '@',
96c8d78f19SGreg Roach            'text'  => view('selects/source', ['source' => $record]),
97852ede8cSGreg Roach            'html'  => view('modals/record-created', [
98852ede8cSGreg Roach                'title' => I18N::translate('The source has been created'),
99852ede8cSGreg Roach                'name'  => $record->fullName(),
100852ede8cSGreg Roach                'url'   => $record->url(),
101852ede8cSGreg Roach            ]),
102852ede8cSGreg Roach        ]);
103852ede8cSGreg Roach    }
104852ede8cSGreg Roach}
105