xref: /webtrees/app/Submission.php (revision bb03c9f048b83092098d5e46c2ab323ae7e2b314)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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;
21
22use Closure;
23use Exception;
24use Fisharebest\Webtrees\Http\RequestHandlers\SubmissionPage;
25use Illuminate\Database\Capsule\Manager as DB;
26
27/**
28 * A GEDCOM submission (SUBN) object.
29 * These records are only used when transferring data between two obsolete systems.
30 * There is no need to create them - but we may encounter them in imported GEDCOM files.
31 */
32class Submission extends GedcomRecord
33{
34    public const RECORD_TYPE = 'SUBN';
35
36    protected const ROUTE_NAME = SubmissionPage::class;
37
38    /**
39     * A closure which will create a record from a database row.
40     *
41     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::submission()
42     *
43     * @param Tree $tree
44     *
45     * @return Closure
46     */
47    public static function rowMapper(Tree $tree): Closure
48    {
49        return Factory::submission()->mapper($tree);
50    }
51
52    /**
53     * Get an instance of a submission object. For single records,
54     * we just receive the XREF. For bulk records (such as lists
55     * and search results) we can receive the GEDCOM data as well.
56     *
57     * @param string      $xref
58     * @param Tree        $tree
59     * @param string|null $gedcom
60     *
61     * @deprecated since 2.0.4.  Will be removed in 2.1.0 - Use Factory::submission()
62     *
63     * @return Submission|null
64     */
65    public static function getInstance(string $xref, Tree $tree, string $gedcom = null): ?Submission
66    {
67        return Factory::submission()->make($xref, $tree, $gedcom);
68    }
69
70    /**
71     * Fetch data from the database
72     *
73     * @param string $xref
74     * @param int    $tree_id
75     *
76     * @return string|null
77     */
78    protected static function fetchGedcomRecord(string $xref, int $tree_id): ?string
79    {
80        return DB::table('other')
81            ->where('o_id', '=', $xref)
82            ->where('o_file', '=', $tree_id)
83            ->where('o_type', '=', self::RECORD_TYPE)
84            ->value('o_gedcom');
85    }
86
87    /**
88     * Generate a private version of this record
89     *
90     * @param int $access_level
91     *
92     * @return string
93     */
94    protected function createPrivateGedcomRecord(int $access_level): string
95    {
96        return '0 @' . $this->xref . "@ SUBM\n1 NAME " . I18N::translate('Private');
97    }
98    /**
99     * Extract names from the GEDCOM record.
100     *
101     * @return void
102     */
103    public function extractNames(): void
104    {
105        $this->getAllNames[] = [
106            'type'   => self::RECORD_TYPE,
107            'sort'   => I18N::translate('Submission'),
108            'full'   => I18N::translate('Submission'),
109            'fullNN' => I18N::translate('Submission'),
110        ];
111    }
112}
113