xref: /webtrees/app/Http/RequestHandlers/EditRawFactAction.php (revision 9db6d3cb480383d6d4c2fa55571321d4e8062394)
13b3db8adSGreg Roach<?php
23b3db8adSGreg Roach
33b3db8adSGreg Roach/**
43b3db8adSGreg Roach * webtrees: online genealogy
519aed3a1SGreg Roach * Copyright (C) 2020 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
153b3db8adSGreg Roach * along with this program. If not, see <http://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;
23a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory;
243b3db8adSGreg Roachuse Fisharebest\Webtrees\Tree;
253b3db8adSGreg Roachuse Psr\Http\Message\ResponseInterface;
263b3db8adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
273b3db8adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
283b3db8adSGreg Roach
293b3db8adSGreg Roachuse function assert;
30ddeb3354SGreg Roachuse function is_string;
313b3db8adSGreg Roachuse function preg_replace;
323b3db8adSGreg Roachuse function redirect;
333b3db8adSGreg Roachuse function trim;
343b3db8adSGreg Roach
353b3db8adSGreg Roach/**
363b3db8adSGreg Roach * Edit the raw GEDCOM of a fact.
373b3db8adSGreg Roach */
383b3db8adSGreg Roachclass EditRawFactAction implements RequestHandlerInterface
393b3db8adSGreg Roach{
403b3db8adSGreg Roach    /**
413b3db8adSGreg Roach     * @param ServerRequestInterface $request
423b3db8adSGreg Roach     *
433b3db8adSGreg Roach     * @return ResponseInterface
443b3db8adSGreg Roach     */
453b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
463b3db8adSGreg Roach    {
473b3db8adSGreg Roach        $tree = $request->getAttribute('tree');
483b3db8adSGreg Roach        assert($tree instanceof Tree);
493b3db8adSGreg Roach
503b3db8adSGreg Roach        $xref = $request->getAttribute('xref');
51ddeb3354SGreg Roach        assert(is_string($xref));
52ddeb3354SGreg Roach
53a091ac74SGreg Roach        $record = Factory::gedcomRecord()->make($xref, $tree);
54*9db6d3cbSGreg Roach        $record = Auth::checkRecordAccess($record, true);
553b3db8adSGreg Roach
563b3db8adSGreg Roach        $fact_id = $request->getAttribute('fact_id');
57*9db6d3cbSGreg Roach        assert(is_string($fact_id));
58b46c87bdSGreg Roach
59b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
60b46c87bdSGreg Roach
61b46c87bdSGreg Roach        $gedcom = $params['gedcom'];
623b3db8adSGreg Roach
633b3db8adSGreg Roach        // Cleanup the client’s bad editing?
643b3db8adSGreg Roach        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom); // Empty lines
653b3db8adSGreg Roach        $gedcom = trim($gedcom); // Leading/trailing spaces
66ddeb3354SGreg Roach        $record = Auth::checkRecordAccess($record, true);
673b3db8adSGreg Roach
6819aed3a1SGreg Roach        foreach ($record->facts([], false, null, true) as $fact) {
6919aed3a1SGreg Roach            if ($fact->id() === $fact_id && $fact->canEdit()) {
703b3db8adSGreg Roach                $record->updateFact($fact_id, $gedcom, false);
713b3db8adSGreg Roach                break;
723b3db8adSGreg Roach            }
733b3db8adSGreg Roach        }
743b3db8adSGreg Roach
75*9db6d3cbSGreg Roach        return redirect($params['url'] ?? $record->url());
763b3db8adSGreg Roach    }
773b3db8adSGreg Roach}
78