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