xref: /webtrees/app/Http/RequestHandlers/EditNoteAction.php (revision 9dce578d99445e6098ac1e8cf1f38367a902bfcd)
1*9dce578dSGreg Roach<?php
2*9dce578dSGreg Roach
3*9dce578dSGreg Roach/**
4*9dce578dSGreg Roach * webtrees: online genealogy
5*9dce578dSGreg Roach * Copyright (C) 2020 webtrees development team
6*9dce578dSGreg Roach * This program is free software: you can redistribute it and/or modify
7*9dce578dSGreg Roach * it under the terms of the GNU General Public License as published by
8*9dce578dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*9dce578dSGreg Roach * (at your option) any later version.
10*9dce578dSGreg Roach * This program is distributed in the hope that it will be useful,
11*9dce578dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*9dce578dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*9dce578dSGreg Roach * GNU General Public License for more details.
14*9dce578dSGreg Roach * You should have received a copy of the GNU General Public License
15*9dce578dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*9dce578dSGreg Roach */
17*9dce578dSGreg Roach
18*9dce578dSGreg Roachdeclare(strict_types=1);
19*9dce578dSGreg Roach
20*9dce578dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*9dce578dSGreg Roach
22*9dce578dSGreg Roachuse Fisharebest\Webtrees\Auth;
23*9dce578dSGreg Roachuse Fisharebest\Webtrees\Registry;
24*9dce578dSGreg Roachuse Fisharebest\Webtrees\Tree;
25*9dce578dSGreg Roachuse Psr\Http\Message\ResponseInterface;
26*9dce578dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27*9dce578dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28*9dce578dSGreg Roach
29*9dce578dSGreg Roachuse function assert;
30*9dce578dSGreg Roach
31*9dce578dSGreg Roach/**
32*9dce578dSGreg Roach * Edit note objects.
33*9dce578dSGreg Roach */
34*9dce578dSGreg Roachclass EditNoteAction implements RequestHandlerInterface
35*9dce578dSGreg Roach{
36*9dce578dSGreg Roach    /**
37*9dce578dSGreg Roach     * Show a form to create a new note object.
38*9dce578dSGreg Roach     *
39*9dce578dSGreg Roach     * @param ServerRequestInterface $request
40*9dce578dSGreg Roach     *
41*9dce578dSGreg Roach     * @return ResponseInterface
42*9dce578dSGreg Roach     */
43*9dce578dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
44*9dce578dSGreg Roach    {
45*9dce578dSGreg Roach        $tree = $request->getAttribute('tree');
46*9dce578dSGreg Roach        assert($tree instanceof Tree);
47*9dce578dSGreg Roach
48*9dce578dSGreg Roach        $xref = $request->getAttribute('xref');
49*9dce578dSGreg Roach
50*9dce578dSGreg Roach        $note = Registry::noteFactory()->make($xref, $tree);
51*9dce578dSGreg Roach        $note = Auth::checkNoteAccess($note, true);
52*9dce578dSGreg Roach
53*9dce578dSGreg Roach        $params = (array) $request->getParsedBody();
54*9dce578dSGreg Roach
55*9dce578dSGreg Roach        $NOTE = $params['NOTE'];
56*9dce578dSGreg Roach
57*9dce578dSGreg Roach        // Convert HTML line endings to GEDCOM continuations
58*9dce578dSGreg Roach        $NOTE = strtr($NOTE, ["\r\n" => "\n1 CONT "]);
59*9dce578dSGreg Roach
60*9dce578dSGreg Roach        // "\" and "$" are significant in preg replacement strings, so escape them.
61*9dce578dSGreg Roach        $NOTE = str_replace(['\\', '$'], ['\\\\', '\\$'], $NOTE);
62*9dce578dSGreg Roach
63*9dce578dSGreg Roach        $gedrec = preg_replace(
64*9dce578dSGreg Roach            '/^0 @' . $note->xref() . '@ NOTE.*(\n1 CONT.*)*/',
65*9dce578dSGreg Roach            '0 @' . $note->xref() . '@ NOTE ' . $NOTE,
66*9dce578dSGreg Roach            $note->gedcom()
67*9dce578dSGreg Roach        );
68*9dce578dSGreg Roach
69*9dce578dSGreg Roach        $note->updateRecord($gedrec, true);
70*9dce578dSGreg Roach
71*9dce578dSGreg Roach        return redirect($note->url());
72*9dce578dSGreg Roach    }
73*9dce578dSGreg Roach}
74