xref: /webtrees/app/Http/RequestHandlers/EditRawRecordAction.php (revision dbfdfe6ba0c8deebeb0f78289edb079de71bb063)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://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\Note;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Tree;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function assert;
33use function explode;
34use function is_string;
35
36/**
37 * Edit the raw GEDCOM of a record.
38 */
39class EditRawRecordAction implements RequestHandlerInterface
40{
41    /**
42     * @param ServerRequestInterface $request
43     *
44     * @return ResponseInterface
45     */
46    public function handle(ServerRequestInterface $request): ResponseInterface
47    {
48        $tree = $request->getAttribute('tree');
49        assert($tree instanceof Tree);
50
51        $xref = $request->getAttribute('xref');
52        assert(is_string($xref));
53
54        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
55        $record = Auth::checkRecordAccess($record, true);
56
57        $params = (array) $request->getParsedBody();
58
59        $level0   = $params['level0'];
60        $facts    = $params['fact'] ?? [];
61        $fact_ids = $params['fact_id'] ?? [];
62
63        // Generate the level-0 line for the record.
64        switch ($record->tag()) {
65            case GedcomRecord::RECORD_TYPE:
66                // Unknown type? - copy the existing data.
67                $gedcom = explode("\n", $record->gedcom(), 2)[0];
68                break;
69            case Header::RECORD_TYPE:
70                $gedcom = '0 HEAD';
71                break;
72            default:
73                $gedcom = '0 @' . $xref . '@ ' . $record->tag();
74        }
75
76        if ($level0 !== '') {
77            $gedcom = $level0;
78        }
79
80        // Retain any private facts
81        foreach ($record->facts([], false, Auth::PRIV_HIDE, true) as $fact) {
82            if (!in_array($fact->id(), $fact_ids, true)) {
83                $gedcom .= "\n" . $fact->gedcom();
84            }
85        }
86        // Append the updated facts
87        foreach ($facts as $fact) {
88            $gedcom .= "\n" . $fact;
89        }
90
91        // Empty lines and MSDOS line endings.
92        $gedcom = preg_replace('/[\r\n]+/', "\n", $gedcom);
93        $gedcom = trim($gedcom);
94
95        $record->updateRecord($gedcom, false);
96
97        return redirect($record->url());
98    }
99}
100