xref: /webtrees/app/Http/RequestHandlers/CreateNoteAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
1d4265d07SGreg Roach<?php
2d4265d07SGreg Roach
3d4265d07SGreg Roach/**
4d4265d07SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d4265d07SGreg Roach * This program is free software: you can redistribute it and/or modify
7d4265d07SGreg Roach * it under the terms of the GNU General Public License as published by
8d4265d07SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d4265d07SGreg Roach * (at your option) any later version.
10d4265d07SGreg Roach * This program is distributed in the hope that it will be useful,
11d4265d07SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d4265d07SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d4265d07SGreg Roach * GNU General Public License for more details.
14d4265d07SGreg 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/>.
16d4265d07SGreg Roach */
17d4265d07SGreg Roach
18d4265d07SGreg Roachdeclare(strict_types=1);
19d4265d07SGreg Roach
20d4265d07SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21d4265d07SGreg Roach
22d4265d07SGreg Roachuse Fisharebest\Webtrees\I18N;
236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
24b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
25d4265d07SGreg Roachuse Psr\Http\Message\ResponseInterface;
26d4265d07SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27d4265d07SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28d4265d07SGreg Roach
29d4265d07SGreg Roach/**
30d4265d07SGreg Roach * Process a form to create a new note object.
31d4265d07SGreg Roach */
32d4265d07SGreg Roachclass CreateNoteAction implements RequestHandlerInterface
33d4265d07SGreg Roach{
34d4265d07SGreg Roach    /**
35d4265d07SGreg Roach     * @param ServerRequestInterface $request
36d4265d07SGreg Roach     *
37d4265d07SGreg Roach     * @return ResponseInterface
38d4265d07SGreg Roach     */
39d4265d07SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
40d4265d07SGreg Roach    {
41b55cbc6bSGreg Roach        $tree        = Validator::attributes($request)->tree();
4203f0aef8SGreg Roach        $note        = Validator::parsedBody($request)->isNotEmpty()->string('note');
4303f0aef8SGreg Roach        $restriction = Validator::parsedBody($request)->string('restriction');
44d4265d07SGreg Roach
4503f0aef8SGreg Roach        $note        = Registry::elementFactory()->make('NOTE:CONT')->canonical($note);
4603f0aef8SGreg Roach        $restriction = Registry::elementFactory()->make('NOTE:RESN')->canonical($restriction);
47d4265d07SGreg Roach
4803f0aef8SGreg Roach        $gedcom = '0 @@ NOTE ' . strtr($note, ["\n" => "\n1 CONT "]);
49d4265d07SGreg Roach
50e3132a19SGreg Roach        if ($restriction !== '') {
5103f0aef8SGreg Roach            $gedcom .= "\n1 RESN " . strtr($restriction, ["\n" => "\n2 CONT "]);
52d4265d07SGreg Roach        }
53d4265d07SGreg Roach
54d4265d07SGreg Roach        $record = $tree->createRecord($gedcom);
55d4265d07SGreg Roach
56c8d78f19SGreg Roach        // value and text are for autocomplete
57d4265d07SGreg Roach        // html is for interactive modals
58d4265d07SGreg Roach        return response([
59c8d78f19SGreg Roach            'value' => '@' . $record->xref() . '@',
60c8d78f19SGreg Roach            'text'  => view('selects/note', ['note' => $record]),
61d4265d07SGreg Roach            'html'  => view('modals/record-created', [
62d4265d07SGreg Roach                'title' => I18N::translate('The note has been created'),
63d4265d07SGreg Roach                'name'  => $record->fullName(),
64d4265d07SGreg Roach                'url'   => $record->url(),
65d4265d07SGreg Roach            ]),
66d4265d07SGreg Roach        ]);
67d4265d07SGreg Roach    }
68d4265d07SGreg Roach}
69