xref: /webtrees/app/Http/RequestHandlers/EditRawRecordAction.php (revision d11be7027e34e3121be11cc025421873364403f9)
13b3db8adSGreg Roach<?php
23b3db8adSGreg Roach
33b3db8adSGreg Roach/**
43b3db8adSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
233b3db8adSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
24ce7d84caSGreg Roachuse Fisharebest\Webtrees\Header;
256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
273b3db8adSGreg Roachuse Psr\Http\Message\ResponseInterface;
283b3db8adSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
293b3db8adSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
303b3db8adSGreg Roach
31ce7d84caSGreg Roachuse function explode;
32c67b9b50SGreg Roachuse function in_array;
33c67b9b50SGreg Roachuse function preg_replace;
34c67b9b50SGreg Roachuse function redirect;
35c67b9b50SGreg Roachuse function trim;
363b3db8adSGreg Roach
373b3db8adSGreg Roach/**
383b3db8adSGreg Roach * Edit the raw GEDCOM of a record.
393b3db8adSGreg Roach */
403b3db8adSGreg Roachclass EditRawRecordAction implements RequestHandlerInterface
413b3db8adSGreg Roach{
423b3db8adSGreg Roach    /**
433b3db8adSGreg Roach     * @param ServerRequestInterface $request
443b3db8adSGreg Roach     *
453b3db8adSGreg Roach     * @return ResponseInterface
463b3db8adSGreg Roach     */
473b3db8adSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
483b3db8adSGreg Roach    {
49b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
50b55cbc6bSGreg Roach        $xref     = Validator::attributes($request)->isXref()->string('xref');
516b9cb339SGreg Roach        $record   = Registry::gedcomRecordFactory()->make($xref, $tree);
52ddeb3354SGreg Roach        $record   = Auth::checkRecordAccess($record, true);
53748dbe15SGreg Roach        $level0   = Validator::parsedBody($request)->string('level0');
54748dbe15SGreg Roach        $facts    = Validator::parsedBody($request)->array('fact');
55748dbe15SGreg Roach        $fact_ids = Validator::parsedBody($request)->array('fact_id');
562917771cSGreg Roach
57ce7d84caSGreg Roach        // Generate the level-0 line for the record.
5802467d32SGreg Roach        switch ($record->tag()) {
59ce7d84caSGreg Roach            case GedcomRecord::RECORD_TYPE:
60ce7d84caSGreg Roach                // Unknown type? - copy the existing data.
61ce7d84caSGreg Roach                $gedcom = explode("\n", $record->gedcom(), 2)[0];
62ce7d84caSGreg Roach                break;
63ce7d84caSGreg Roach            case Header::RECORD_TYPE:
64ce7d84caSGreg Roach                $gedcom = '0 HEAD';
65ce7d84caSGreg Roach                break;
66ce7d84caSGreg Roach            default:
6702467d32SGreg Roach                $gedcom = '0 @' . $xref . '@ ' . $record->tag();
68ce7d84caSGreg Roach        }
693b3db8adSGreg Roach
704c3814d0SGreg Roach        if ($level0 !== '') {
714c3814d0SGreg Roach            $gedcom = $level0;
724c3814d0SGreg Roach        }
734c3814d0SGreg Roach
743b3db8adSGreg Roach        // Retain any private facts
7519aed3a1SGreg Roach        foreach ($record->facts([], false, Auth::PRIV_HIDE, true) as $fact) {
7619aed3a1SGreg Roach            if (!in_array($fact->id(), $fact_ids, true)) {
773b3db8adSGreg Roach                $gedcom .= "\n" . $fact->gedcom();
783b3db8adSGreg Roach            }
793b3db8adSGreg Roach        }
803b3db8adSGreg Roach        // Append the updated facts
813b3db8adSGreg Roach        foreach ($facts as $fact) {
82c67b9b50SGreg Roach            $gedcom .= "\n" . trim($fact);
833b3db8adSGreg Roach        }
843b3db8adSGreg Roach
853b3db8adSGreg Roach        // Empty lines and MSDOS line endings.
863b3db8adSGreg Roach        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
873b3db8adSGreg Roach        $gedcom = trim($gedcom);
883b3db8adSGreg Roach
893b3db8adSGreg Roach        $record->updateRecord($gedcom, false);
903b3db8adSGreg Roach
913b3db8adSGreg Roach        return redirect($record->url());
923b3db8adSGreg Roach    }
933b3db8adSGreg Roach}
94