xref: /webtrees/app/Elements/NamePersonal.php (revision feb494fa63ca05c7c234315ad97cb115e1e81b0b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\Registry;
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     * AbstractGedcomElement constructor.
90     *
91     * @param string             $label
92     * @param array<string>|null $subtags
93     */
94    public function __construct(string $label, array $subtags = null)
95    {
96        if ($subtags === null && in_array(I18N::languageTag(), static::SURNAME_FIRST_LANGUAGES, true)) {
97            $subtags = static::SUBTAGS_SURNAME_FIRST;
98        }
99        parent::__construct($label, $subtags);
100    }
101
102    /**
103     * Create a default value for this element.
104     *
105     * @param Tree $tree
106     *
107     * @return string
108     */
109    public function default(Tree $tree): string
110    {
111        return Registry::surnameTraditionFactory()
112            ->make($tree->getPreference('SURNAME_TRADITION'))
113            ->defaultName();
114    }
115
116    /**
117     * An edit control for this data.
118     *
119     * @param string $id
120     * @param string $name
121     * @param string $value
122     * @param Tree   $tree
123     *
124     * @return string
125     */
126    public function edit(string $id, string $name, string $value, Tree $tree): string
127    {
128        return
129            '<div class="input-group">' .
130            view('edit/input-addon-edit-name', ['id' => $id]) .
131            '<input class="form-control" type="text" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" readonly="readonly" />' .
132            view('edit/input-addon-keyboard', ['id' => $id]) .
133            view('edit/input-addon-help', ['topic' => 'NAME']) .
134            '</div>';
135    }
136}
137