xref: /webtrees/app/Http/RequestHandlers/EditRawRecordAction.php (revision b46c87bda4b592cf9252f1db48552a820b1e3d97)
13b3db8adSGreg Roach<?php
23b3db8adSGreg Roach
33b3db8adSGreg Roach/**
43b3db8adSGreg Roach * webtrees: online genealogy
53b3db8adSGreg Roach * Copyright (C) 2019 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;
233b3db8adSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
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 Roach
323b3db8adSGreg Roach/**
333b3db8adSGreg Roach * Edit the raw GEDCOM of a record.
343b3db8adSGreg Roach */
353b3db8adSGreg Roachclass EditRawRecordAction implements RequestHandlerInterface
363b3db8adSGreg Roach{
373b3db8adSGreg Roach    /**
383b3db8adSGreg Roach     * @param ServerRequestInterface $request
393b3db8adSGreg Roach     *
403b3db8adSGreg Roach     * @return ResponseInterface
413b3db8adSGreg Roach     */
423b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
433b3db8adSGreg Roach    {
443b3db8adSGreg Roach        $tree = $request->getAttribute('tree');
453b3db8adSGreg Roach        assert($tree instanceof Tree);
463b3db8adSGreg Roach
472917771cSGreg Roach        $xref = $request->getAttribute('xref');
48ddeb3354SGreg Roach        assert(is_string($xref));
493b3db8adSGreg Roach
50ddeb3354SGreg Roach        $record = GedcomRecord::getInstance($xref, $tree);
51ddeb3354SGreg Roach        $record = Auth::checkRecordAccess($record, true);
523b3db8adSGreg Roach
53*b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
54*b46c87bdSGreg Roach
55*b46c87bdSGreg Roach        $facts    = $params['fact'] ?? [];
56*b46c87bdSGreg Roach        $fact_ids = $params['fact_id'] ?? [];
572917771cSGreg Roach
583b3db8adSGreg Roach        $gedcom = '0 @' . $record->xref() . '@ ' . $record::RECORD_TYPE;
593b3db8adSGreg Roach
603b3db8adSGreg Roach        // Retain any private facts
613b3db8adSGreg Roach        foreach ($record->facts([], false, Auth::PRIV_HIDE) as $fact) {
623b3db8adSGreg Roach            if (!in_array($fact->id(), $fact_ids, true) && !$fact->isPendingDeletion()) {
633b3db8adSGreg Roach                $gedcom .= "\n" . $fact->gedcom();
643b3db8adSGreg Roach            }
653b3db8adSGreg Roach        }
663b3db8adSGreg Roach        // Append the updated facts
673b3db8adSGreg Roach        foreach ($facts as $fact) {
683b3db8adSGreg Roach            $gedcom .= "\n" . $fact;
693b3db8adSGreg Roach        }
703b3db8adSGreg Roach
713b3db8adSGreg Roach        // Empty lines and MSDOS line endings.
723b3db8adSGreg Roach        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
733b3db8adSGreg Roach        $gedcom = trim($gedcom);
743b3db8adSGreg Roach
753b3db8adSGreg Roach        $record->updateRecord($gedcom, false);
763b3db8adSGreg Roach
773b3db8adSGreg Roach        return redirect($record->url());
783b3db8adSGreg Roach    }
793b3db8adSGreg Roach}
80