xref: /webtrees/app/Http/RequestHandlers/EditRawFactAction.php (revision f507cef925e69f3dc0d47102f965899785571595)
13b3db8adSGreg Roach<?php
23b3db8adSGreg Roach
33b3db8adSGreg Roach/**
43b3db8adSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
63b3db8adSGreg Roach * This program is free software: you can redistribute it and/or modify
73b3db8adSGreg Roach * it under the terms of the GNU General Public License as published by
83b3db8adSGreg Roach * the Free Software Foundation, either version 3 of the License, or
93b3db8adSGreg Roach * (at your option) any later version.
103b3db8adSGreg Roach * This program is distributed in the hope that it will be useful,
113b3db8adSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123b3db8adSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133b3db8adSGreg Roach * GNU General Public License for more details.
143b3db8adSGreg 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/>.
163b3db8adSGreg Roach */
173b3db8adSGreg Roach
183b3db8adSGreg Roachdeclare(strict_types=1);
193b3db8adSGreg Roach
203b3db8adSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
213b3db8adSGreg Roach
223b3db8adSGreg Roachuse Fisharebest\Webtrees\Auth;
236b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
248d9c2b68SGreg Roachuse Fisharebest\Webtrees\Validator;
253b3db8adSGreg Roachuse Psr\Http\Message\ResponseInterface;
263b3db8adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
273b3db8adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
283b3db8adSGreg Roach
293b3db8adSGreg Roachuse function preg_replace;
303b3db8adSGreg Roachuse function redirect;
313b3db8adSGreg Roachuse function trim;
323b3db8adSGreg Roach
333b3db8adSGreg Roach/**
343b3db8adSGreg Roach * Edit the raw GEDCOM of a fact.
353b3db8adSGreg Roach */
363b3db8adSGreg Roachclass EditRawFactAction implements RequestHandlerInterface
373b3db8adSGreg Roach{
383b3db8adSGreg Roach    /**
393b3db8adSGreg Roach     * @param ServerRequestInterface $request
403b3db8adSGreg Roach     *
413b3db8adSGreg Roach     * @return ResponseInterface
423b3db8adSGreg Roach     */
433b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
443b3db8adSGreg Roach    {
45b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
46b55cbc6bSGreg Roach        $xref    = Validator::attributes($request)->isXref()->string('xref');
476b9cb339SGreg Roach        $record  = Registry::gedcomRecordFactory()->make($xref, $tree);
489db6d3cbSGreg Roach        $record  = Auth::checkRecordAccess($record, true);
49b55cbc6bSGreg Roach        $fact_id = Validator::attributes($request)->string('fact_id');
50b46c87bdSGreg Roach        $params  = (array) $request->getParsedBody();
51b46c87bdSGreg Roach        $gedcom  = $params['gedcom'];
523b3db8adSGreg Roach
533b3db8adSGreg Roach        // Cleanup the client’s bad editing?
543b3db8adSGreg Roach        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); // Empty lines
553b3db8adSGreg Roach        $gedcom = trim($gedcom); // Leading/trailing spaces
56ddeb3354SGreg Roach        $record = Auth::checkRecordAccess($record, true);
573b3db8adSGreg Roach
5819aed3a1SGreg Roach        foreach ($record->facts([], false, null, true) as $fact) {
5919aed3a1SGreg Roach            if ($fact->id() === $fact_id && $fact->canEdit()) {
603b3db8adSGreg Roach                $record->updateFact($fact_id, $gedcom, false);
613b3db8adSGreg Roach                break;
623b3db8adSGreg Roach            }
633b3db8adSGreg Roach        }
643b3db8adSGreg Roach
65*f507cef9SGreg Roach        $url = Validator::parsedBody($request)->isLocalUrl()->string('url', $record->url());
66551ad4afSGreg Roach
67551ad4afSGreg Roach        return redirect($url);
683b3db8adSGreg Roach    }
693b3db8adSGreg Roach}
70