xref: /webtrees/app/Http/RequestHandlers/EditNoteAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
19dce578dSGreg Roach<?php
29dce578dSGreg Roach
39dce578dSGreg Roach/**
49dce578dSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
69dce578dSGreg Roach * This program is free software: you can redistribute it and/or modify
79dce578dSGreg Roach * it under the terms of the GNU General Public License as published by
89dce578dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
99dce578dSGreg Roach * (at your option) any later version.
109dce578dSGreg Roach * This program is distributed in the hope that it will be useful,
119dce578dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
129dce578dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139dce578dSGreg Roach * GNU General Public License for more details.
149dce578dSGreg 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/>.
169dce578dSGreg Roach */
179dce578dSGreg Roach
189dce578dSGreg Roachdeclare(strict_types=1);
199dce578dSGreg Roach
209dce578dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
219dce578dSGreg Roach
229dce578dSGreg Roachuse Fisharebest\Webtrees\Auth;
239dce578dSGreg Roachuse Fisharebest\Webtrees\Registry;
24b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
259dce578dSGreg Roachuse Psr\Http\Message\ResponseInterface;
269dce578dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
279dce578dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
289dce578dSGreg Roach
299dce578dSGreg Roach/**
309dce578dSGreg Roach * Edit note objects.
319dce578dSGreg Roach */
329dce578dSGreg Roachclass EditNoteAction implements RequestHandlerInterface
339dce578dSGreg Roach{
349dce578dSGreg Roach    /**
359dce578dSGreg Roach     * Show a form to create a new note object.
369dce578dSGreg Roach     *
379dce578dSGreg Roach     * @param ServerRequestInterface $request
389dce578dSGreg Roach     *
399dce578dSGreg Roach     * @return ResponseInterface
409dce578dSGreg Roach     */
419dce578dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
429dce578dSGreg Roach    {
43b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
44b55cbc6bSGreg Roach        $xref = Validator::attributes($request)->isXref()->string('xref');
459dce578dSGreg Roach        $note = Registry::noteFactory()->make($xref, $tree);
469dce578dSGreg Roach        $note = Auth::checkNoteAccess($note, true);
47748dbe15SGreg Roach        $NOTE = Validator::parsedBody($request)->string('NOTE');
489dce578dSGreg Roach
499dce578dSGreg Roach        // Convert HTML line endings to GEDCOM continuations
509dce578dSGreg Roach        $NOTE = strtr($NOTE, ["\r\n" => "\n1 CONT "]);
519dce578dSGreg Roach
529dce578dSGreg Roach        // "\" and "$" are significant in preg replacement strings, so escape them.
539dce578dSGreg Roach        $NOTE = str_replace(['\\', '$'], ['\\\\', '\\$'], $NOTE);
549dce578dSGreg Roach
559dce578dSGreg Roach        $gedrec = preg_replace(
569dce578dSGreg Roach            '/^0 @' . $note->xref() . '@ NOTE.*(\n1 CONT.*)*/',
579dce578dSGreg Roach            '0 @' . $note->xref() . '@ NOTE ' . $NOTE,
589dce578dSGreg Roach            $note->gedcom()
599dce578dSGreg Roach        );
609dce578dSGreg Roach
619dce578dSGreg Roach        $note->updateRecord($gedrec, true);
629dce578dSGreg Roach
639dce578dSGreg Roach        return redirect($note->url());
649dce578dSGreg Roach    }
659dce578dSGreg Roach}
66