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