Issues (2502)

app/Http/RequestHandlers/CreateSourceAction.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Validator;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
final class CreateSourceAction implements RequestHandlerInterface
30
{
31
    public function handle(ServerRequestInterface $request): ResponseInterface
32
    {
33
        $tree         = Validator::attributes($request)->tree();
34
        $title        = Validator::parsedBody($request)->isNotEmpty()->string('source-title');
35
        $abbreviation = Validator::parsedBody($request)->string('source-abbreviation');
36
        $author       = Validator::parsedBody($request)->string('source-author');
37
        $publication  = Validator::parsedBody($request)->string('source-publication');
38
        $repository   = Validator::parsedBody($request)->isXref()->string('source-repository', '');
39
        $call_number  = Validator::parsedBody($request)->string('source-call-number');
40
        $text         = Validator::parsedBody($request)->string('source-text');
41
        $restriction  = Validator::parsedBody($request)->string('restriction');
42
43
        $title        = Registry::elementFactory()->make('SOUR:TITL')->canonical($title);
44
        $abbreviation = Registry::elementFactory()->make('SOUR:ABBR')->canonical($abbreviation);
45
        $author       = Registry::elementFactory()->make('SOUR:AUTH')->canonical($author);
46
        $publication  = Registry::elementFactory()->make('SOUR:PUBL')->canonical($publication);
47
        $repository   = Registry::elementFactory()->make('SOUR:REPO')->canonical($repository);
48
        $call_number  = Registry::elementFactory()->make('SOUR:REPO:CALN')->canonical($call_number);
49
        $text         = Registry::elementFactory()->make('SOUR:TEXT')->canonical($text);
50
        $restriction  = Registry::elementFactory()->make('SOUR:RESN')->canonical($restriction);
51
52
        $gedcom = "0 @@ SOUR\n1 TITL " . strtr($title, ["\n" => "\n2 CONT "]);
53
54
        if ($abbreviation !== '') {
55
            $gedcom .= "\n1 ABBR " . strtr($abbreviation, ["\n" => "\n2 CONT "]);
56
        }
57
58
        if ($author !== '') {
59
            $gedcom .= "\n1 AUTH " . strtr($author, ["\n" => "\n2 CONT "]);
60
        }
61
62
        if ($publication !== '') {
63
            $gedcom .= "\n1 PUBL " . strtr($publication, ["\n" => "\n2 CONT "]);
64
        }
65
66
        if ($text !== '') {
67
            $gedcom .= "\n1 TEXT " . strtr($text, ["\n" => "\n2 CONT "]);
68
        }
69
70
        if ($repository !== '') {
71
            $gedcom .= "\n1 REPO @" . $repository . '@';
72
73
            if ($call_number !== '') {
74
                $gedcom .= "\n2 CALN " . strtr($call_number, ["\n" => "\n3 CONT "]);
75
            }
76
        }
77
78
        if ($restriction !== '') {
79
            $gedcom .= "\n1 RESN " . strtr($restriction, ["\n" => "\n2 CONT "]);
80
        }
81
82
        $record = $tree->createRecord($gedcom);
83
84
        // value and text are for autocomplete
85
        // html is for interactive modals
86
        return response([
87
            'value' => '@' . $record->xref() . '@',
88
            'text'  => view('selects/source', ['source' => $record]),
89
            'html'  => view('modals/record-created', [
90
                'title' => I18N::translate('The source has been created'),
91
                'name'  => $record->fullName(),
92
                'url'   => $record->url(),
93
            ]),
94
        ]);
95
    }
96
}
97