Passed
Push — master ( 852ede...d4265d )
by Greg
06:08
created

EditNoteController::createNoteObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\Controllers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Note;
25
use Fisharebest\Webtrees\Tree;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
29
use function assert;
30
31
/**
32
 * Controller for edit forms and responses.
33
 */
34
class EditNoteController extends AbstractEditController
35
{
36
    /**
37
     * Show a form to create a new note object.
38
     *
39
     * @param ServerRequestInterface $request
40
     *
41
     * @return ResponseInterface
42
     */
43
    public function editNoteObject(ServerRequestInterface $request): ResponseInterface
44
    {
45
        $tree = $request->getAttribute('tree');
46
        assert($tree instanceof Tree);
47
48
        $xref = $request->getQueryParams()['xref'];
49
50
        $note = Note::getInstance($xref, $tree);
51
        $note = Auth::checkNoteAccess($note, true);
52
53
        return $this->viewResponse('edit/shared-note', [
54
            'note'  => $note,
55
            'title' => I18N::translate('Edit the shared note'),
56
            'tree'  => $tree,
57
        ]);
58
    }
59
60
    /**
61
     * Show a form to create a new note object.
62
     *
63
     * @param ServerRequestInterface $request
64
     *
65
     * @return ResponseInterface
66
     */
67
    public function updateNoteObject(ServerRequestInterface $request): ResponseInterface
68
    {
69
        $tree = $request->getAttribute('tree');
70
        assert($tree instanceof Tree);
71
72
        $xref = $request->getQueryParams()['xref'];
73
74
        $note = Note::getInstance($xref, $tree);
75
        $note = Auth::checkNoteAccess($note, true);
76
77
        $NOTE = $request->getParsedBody()['NOTE'];
78
79
        // "\" and "$" are signficant in replacement strings, so escape them.
80
        $NOTE = str_replace([
81
            '\\',
82
            '$',
83
        ], [
84
            '\\\\',
85
            '\\$',
86
        ], $NOTE);
87
88
        $gedrec = preg_replace(
89
            '/^0 @' . $note->xref() . '@ NOTE.*(\n1 CONT.*)*/',
90
            '0 @' . $note->xref() . '@ NOTE ' . preg_replace("/\r?\n/", "\n1 CONT ", $NOTE),
91
            $note->gedcom()
92
        );
93
94
        $note->updateRecord($gedrec, true);
95
96
        return redirect($note->url());
97
    }
98
}
99