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