xref: /webtrees/app/Elements/NamePersonal.php (revision 2ebcf907ed34213f816592af04e6c160335d6311)
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\Elements;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\SurnameTradition;
24use Fisharebest\Webtrees\Tree;
25
26use function e;
27use function in_array;
28use function view;
29
30/**
31 * NAME_PERSONAL := {Size=1:120}
32 * [
33 * <NAME_TEXT> | /<NAME_TEXT>/ |
34 * <NAME_TEXT> /<NAME_TEXT>/ | /<NAME_TEXT>/ <NAME_TEXT> |
35 * <NAME_TEXT> /<NAME_TEXT>/ <NAME_TEXT> ]
36 * The surname of an individual, if known, is enclosed between two slash (/)
37 * characters. The order of the name parts should be the order that the person
38 * would, by custom of their culture, have used when giving it to a recorder.
39 * Early versions of Personal Ancestral File ® and other products did not use
40 * the trailing slash when the surname was the last element of the name. If
41 * part of name is illegible, that part is indicated by an ellipsis (...).
42 * Capitalize the name of a person or place in the conventional manner—
43 * capitalize the first letter of each part and lowercase the other letters,
44 * unless conventional usage is otherwise. For example: McMurray.
45 * Examples:
46 * William Lee (given name only or surname not known)
47 * /Parry/ (surname only)
48 * William Lee /Parry/
49 * William Lee /Mac Parry/ (both parts (Mac and Parry) are surname parts
50 * William /Lee/ Parry (surname imbedded in the name string)
51 * William Lee /Pa.../
52 */
53class NamePersonal extends AbstractElement
54{
55    protected const MAXIMUM_LENGTH = 120;
56
57    protected const SUBTAGS = [
58        'TYPE' => '0:1',
59        'NPFX' => '0:1',
60        'GIVN' => '0:1',
61        'SPFX' => '0:1',
62        'SURN' => '0:1',
63        'NSFX' => '0:1',
64        'NICK' => '0:1',
65        'NOTE' => '0:M',
66        'SOUR' => '0:M',
67        'FONE' => '0:M',
68        'ROMN' => '0:M',
69    ];
70
71    // For some languages, we want to show the surname field first.
72    protected const SURNAME_FIRST_LANGUAGES = ['hu', 'jp', 'ko', 'zh-Hans', 'zh-Hant'];
73
74    protected const SUBTAGS_SURNAME_FIRST = [
75        'TYPE' => '0:1',
76        'NPFX' => '0:1',
77        'SPFX' => '0:1',
78        'SURN' => '0:1',
79        'GIVN' => '0:1',
80        'NSFX' => '0:1',
81        'NICK' => '0:1',
82        'NOTE' => '0:M',
83        'SOUR' => '0:M',
84        'FONE' => '0:M',
85        'ROMN' => '0:M',
86    ];
87
88    /**
89     * Create a default value for this element.
90     *
91     * @param Tree $tree
92     *
93     * @return string
94     */
95    public function default(Tree $tree): string
96    {
97        $surname_tradition = SurnameTradition::create($tree->getPreference('SURNAME_TRADITION'));
98
99        if ($surname_tradition->hasSurnames()) {
100            return '//';
101        }
102
103        return '';
104    }
105
106    /**
107     * An edit control for this data.
108     *
109     * @param string $id
110     * @param string $name
111     * @param string $value
112     * @param Tree   $tree
113     *
114     * @return string
115     */
116    public function edit(string $id, string $name, string $value, Tree $tree): string
117    {
118        return
119            '<div class="input-group">' .
120            view('edit/input-addon-edit-name', ['id' => $id]) .
121            '<input class="form-control" type="text" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" readonly="readonly" />' .
122            view('edit/input-addon-keyboard', ['id' => $id]) .
123            view('edit/input-addon-help', ['fact' => 'NAME']) .
124            '</div>';
125    }
126
127    /**
128     * @return array<string,string>
129     */
130    public function subtags(): array
131    {
132        if (in_array(I18N::languageTag(), static::SURNAME_FIRST_LANGUAGES, true)) {
133            return static::SUBTAGS_SURNAME_FIRST;
134        }
135
136        return static::SUBTAGS;
137    }
138}
139