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