xref: /webtrees/app/Http/RequestHandlers/EditRawRecordAction.php (revision b5961194694c0b5b2dc4269689207eb972e3b20c)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\Header;
25use Fisharebest\Webtrees\Tree;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\RequestHandlerInterface;
29
30use function assert;
31use function explode;
32use function is_string;
33
34/**
35 * Edit the raw GEDCOM of a record.
36 */
37class EditRawRecordAction implements RequestHandlerInterface
38{
39    /**
40     * @param ServerRequestInterface $request
41     *
42     * @return ResponseInterface
43     */
44    public function handle(ServerRequestInterface $request): ResponseInterface
45    {
46        $tree = $request->getAttribute('tree');
47        assert($tree instanceof Tree);
48
49        $xref = $request->getAttribute('xref');
50        assert(is_string($xref));
51
52        $record = GedcomRecord::getInstance($xref, $tree);
53        $record = Auth::checkRecordAccess($record, true);
54
55        $params = (array) $request->getParsedBody();
56
57        $facts    = $params['fact'] ?? [];
58        $fact_ids = $params['fact_id'] ?? [];
59
60        // Generate the level-0 line for the record.
61        switch ($record::RECORD_TYPE) {
62            case GedcomRecord::RECORD_TYPE:
63                // Unknown type? - copy the existing data.
64                $gedcom = explode("\n", $record->gedcom(), 2)[0];
65                break;
66            case Header::RECORD_TYPE:
67                $gedcom = '0 HEAD';
68                break;
69            default:
70                $gedcom = '0 @' . $xref . '@ ' . $record::RECORD_TYPE;
71        }
72
73        // Retain any private facts
74        foreach ($record->facts([], false, Auth::PRIV_HIDE, true) as $fact) {
75            if (!in_array($fact->id(), $fact_ids, true)) {
76                $gedcom .= "\n" . $fact->gedcom();
77            }
78        }
79        // Append the updated facts
80        foreach ($facts as $fact) {
81            $gedcom .= "\n" . $fact;
82        }
83
84        // Empty lines and MSDOS line endings.
85        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
86        $gedcom = trim($gedcom);
87
88        $record->updateRecord($gedcom, false);
89
90        return redirect($record->url());
91    }
92}
93